Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] [Fwd: Re: For Each]

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-