'Foreach questions
[1/4] from: tim::johnsons-web::com at: 21-Dec-2002 11:29
Fellow Rebols:
I'm trying to use the same block of words in both a query
and in parsing data from the query. To illustrate here's some
test data, some results and some results I'm looking for...
; need some words set to literal values
>> col-names: set [a b c d] ["one" "two" "three" "four"]
== ["one" "two" "three" "four"]
; now some test data. Let's pretend that this data is retrieved
; by a query using col-names above
>> data: [[1 2 3 4] [5 6 7 8] [9 10 11 12]]
== [[1 2 3 4] [5 6 7 8] [9 10 11 12]]
; now I want to fetch each inner block in 'data and
; automagically set each word in 'col-names to each successive
; item in the inner block
>> foreach row data[set 'col-names row
prin mold col-names prin mold a
prin mold b prin mold c print mold d
]
[1 2 3 4]"one""two""three""four"
[5 6 7 8]"one""two""three""four"
[9 10 11 12]"one""two""three""four"
; alas, I've failed to get what I wanted. What I wanted to see is:
[1 2 3 4] 1 2 3 4
[5 6 7 8] 5 6 7 8
[9 10 11 12] 9 10 11 12
; where each 'word originally in col-names would be set to
; a return value
Any ideas on how I can make this work for me in a similar
fashion.
TIA
--
Tim Johnson <[tim--johnsons-web--com]>
http://www.alaska-internet-solutions.com
http://www.johnsons-web.com
[2/4] from: brett:codeconscious at: 22-Dec-2002 8:47
Hi Tim,
After this line:
col-names: set [a b c d] ["one" "two" "three" "four"]
A will be "one", B will be "two", etc.,.
col-names will be the block of strings ["one" "two" "three" "four"] not [a b
c d].
Inside your foreach loop you are using
set 'col-names row
So you are setting the *word* col-names itself to each of the data blocks in
turn - again nothing to do with A, B, C, D. That is why a, b, c, d do not
change each row as you print them.
This is probably closer to what you want:
columns: [a b c d]
col-names: ["one" "two" "three" "four"]
data: [[1 2 3 4] [5 6 7 8] [9 10 11 12]]
foreach row data [
set columns row
prin mold col-names prin mold a
prin mold b prin mold c print mold d
]
or even:
columns: [a b c d]
col-names: ["one" "two" "three" "four"]
data: [[1 2 3 4] [5 6 7 8] [9 10 11 12]]
foreach row data [
set columns row
print [mold col-names mold reduce columns]
]
Regards,
Brett.
[3/4] from: gerardcote:sympatico:ca at: 21-Dec-2002 18:20
Hi Tim,
here is my second attempt to answer your problem :
>> foreach row data[
[ set 'col-names row
[ prin mold row print col-names]
[1 2 3 4]1 2 3 4
[5 6 7 8]5 6 7 8
[9 10 11 12]9 10 11 12
>>
Hope that helps!
Regards,
Gerard
P.S. I'm currently coming back to real life ...
[4/4] from: tim:johnsons-web at: 21-Dec-2002 15:23
Thanks to Gerard and Matt:
I think I'm finding a work-around. What I really
want to do is maintain a close syntactical link between
data requested and data retrieved.
What I might need is a function called 'form-block
; take a block of words and returns a block of strings
form-block: func[blk[block!]][
blk1: make block! length? blk
foreach b blk[append blk1 form b]
blk1
]
columns: [exp_date first_name last_name age]
>> form-block columns
== ["exp_date" "first_name" "last_name" "age"]
data: db[select (form-block columns) from table where "ID" eq ID] ; untested of course
;returns for example date
data: [[1 2 3 4] [5 6 7 8] [9 10 11 12]]
>> foreach row data[set columns row print [exp_date first_name last_name age]]
1 2 3 4
5 6 7 8
9 10 11 12
; that should get me going... thanks!
-tj-
* Gerard Cote <[gerardcote--sympatico--ca]> [021221 14:50]:
> Hi Tim,
> here is my second attempt to answer your problem :
<<quoted lines omitted: 60>>
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.
--
Tim Johnson <[tim--johnsons-web--com]>
http://www.alaska-internet-solutions.com
http://www.johnsons-web.com
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted