For Each
[1/5] from: ammoncooke::yahoo::com at: 20-Jun-2001 21:22
Hi,
I need to 'for each' thru a text-list, what is the correct syntax??
I have this:
lst1-len: length? head lst1/data
for i 1 lst1-len 1[
Which will execute the correct number of times, but how do I get a
different value from the list each time like I can in Perl with an array
using the index number??
Thanks!!
Ammon
[2/5] from: allen:aussieweb:au at: 21-Jun-2001 13:51
Hi Ammon,
The various looping options are described here in the manual
http://www.rebol.com/docs/core23/rebolcore-4.html#pgfId-839434
Why not just use 'foreach ?
foreach line read/lines %text.txt [print line]
or is there a reason you need the index number?
Cheers,
Allen K
[3/5] from: joel:neely:fedex at: 21-Jun-2001 2:13
Hi, Ammon,
Having learned REBOL after Perl myself, maybe I can offer a
few "quick translation" hints...
> I need to 'for each' thru a text-list, what is the correct
> syntax??
>
If you'll pardon my being picky, I wanted to mention that one
of the first "Aha!" experiences I had with REBOL was realizing
that it uses very little "syntax", in the conventional sense.
There is syntax for literals of the various data types (e.g.
1.2
is a DECIMAL! while
1.2.3
is a TUPLE!), but beyond that everything is just expressions
with literals (including words), operators, and functions.
The reason I belabor this point is that it makes it easier to
understand some of the more interesting code you'll find in
the example library.
> I have this:
>
> lst1-len: length? head lst1/data
> for i 1 lst1-len 1[
>
> Which will execute the correct number of times, but how do
> I get a different value from the list each time like I can
> in Perl with an array using the index number??
>
Do it just as you would in Perl (TMTOWTDI!) with the REBOL
functions that correspond to the Perl statements:
1a) If you just want the individual values, and don't really
care about the indexes, you'd write this in Perl as
foreach $item (@array) {
#... do something with $item ...
}
The corresponding REBOL function (using your sample) is
foreach item lst1/data [
;... do something with ITEM ...
]
1b) If you don't even care about preserving the array in
Perl, you might write it as
while ($item = shift @array) {
#... do something with $item
}
which could be expressed (sort of ;-) with the REBOL function
forall blockvar [
;... do something with blockvar/1
;... or use first blockvar
]
except that REBOL lets you say
blockvar: head blockvar
to "restore" the block.
2) If you just want to know how to get value based on their
positions (indexes), you'd use the Perl expression
$array[$i]
to access the element of @array at position $i . In REBOL
you can use PICK and POKE to get and set values in a block:
pick blockval i
and
poke blockval i newval
but you MUST use indexes that already exist! (Perl will let
you extend an array by simply storing something in the "next"
index position, but REBOL requires you to APPEND to a block
if you really want to increase its length.)
You can also fetch values from a block via position by using
a path with an embedded get-word in it, as in
blockvar/:indexvar
to get the element at the position identified by INDEXVAR.
Now for the controlling functions.
3a) If you want to iterate over the index values in Perl,
you'd say something like
for ($i = 0; $i < @array; ++$i) {
#... do something with $array[$i] ...
}
The most direct REBOL analogue is the FOR function, as you
suspected:
for i 1 length? lst1/data 1 [
#... use PICK/POKE or LST1/DATA/:I
]
But that's overkill if you really just want to
3b) Don't forget that REBOL positions always start at 1
(instead of the 0 origin commonly used in other languages).
repeat i length? lst1/data [
#... use I as the index (as above)
]
which will start I counting from 1 and step by 1 through the
maximum count (LENGTH? LST1/DATA in this example).
There are a few more variations, but those should get you
through the most common cases.
HTH!
-jn-
[4/5] from: dness:home at: 21-Jun-2001 9:42
Joel Neely wrote:
> Hi, Ammon,
> Having learned REBOL after Perl myself, maybe I can offer a
<<quoted lines omitted: 88>>
> HTH!
> -jn-
I find it very helpful. I would think it useful to collect `notes' about
this kind of transl(iter)ation from perl and other languages. For those of
us who come to REBOL after some substantial experience with other languages
it is a big help.
[5/5] from: ammoncooke:y:ahoo at: 24-Jun-2001 18:42
Thanks!!
foreach item lst1/data[
]
Will do the trick for me. I just didn't know the right syntax.
Ammon
PS I definetly like the idea of transliteration. ;)
Joel Neely wrote:
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted