Re: foreach examples/usage
On Tue, 17 Jul 2001, Evan Panagiotopoulos wrote: > I have an array of X elements and I want to go from the first element > (is it zero?) to the last. I have a variable containing the number of > elements in the array. If the array has 22 elements does it go from 0 > to 21 or 1 to 22? foreach doesn't need to know the number of elements - it just goes from the first element to the last e.g. #!/usr/bin/perl -w my @array = (1,2,3,4,5,6,7,8,9); # explicit scalar variable foreach my $item ( @array ) { print "$item"; } # newline for tidiness print "\n"; # do it again using the implicit $_ variable foreach ( @array ) { print; } # newline for tidiness print "\n"; This script prints out: 123456789 123456789 Best wishes, Rachel Coleman -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: foreach examples/usage
Evan Panagiotopoulos wrote: > > I am trying to learn the foreach loop. Is there a place on the web where there are >examples for the foreach? Basically I have an array of X elements and I > want to go >from the first element (is it zero?) Yes! > to the last. I have a variable containing the number of elements in the array. > This can also be obtained from the array itself. You've got an arrary of 22 elements (@array) - the last index will be $#array (ie 21) But you don't *need* to know this for a foreach. foreach my $num (@array) { # do somthing with $num } Alternatively.. foreach (@array) { # do something with $_ } If you *needed* to know the index for each element, you'd probably need a for loop. for (my $i = 0; $i<=$#array; $i++) { # do something with $array[$i] } If you wanted to preserve @array, and make a @new_array, use map - a bit more convenient than a foreach ... { push } my @new_array = map #do something with $_ here#, @array; eg: my @new_array = map $_*2 , @array; Would give 0,2,4,6,8,10,12,14,16.42,44 -- David Wood, Web Developer [a] Clickmusic Ltd, 99c Talbot Road, Notting Hill, London W11 2AT [t] 020 7727 7500 [w] www.clickmusic.co.uk "There are three types of people in the world; those who can count, and those who can't."
Re: foreach examples/usage
Sounds interesting. Please verify this link. -- Original Message -- From: "Jos I. Boumans" <[EMAIL PROTECTED]> Date: Sun, 8 Jul 2001 11:19:21 +0200 > >you might find the loops/block tutorial i wrote on >www.sharemation.com/~perl/tut helpfull > >regards, > >Jos Boumans > > >I am trying to learn the foreach loop. Is there a place on the web where >there are examples for the foreach? Basically I have an array of X elements >and I want to go from the first element (is it zero?) to the last. I have a >variable containing the number of elements in the array. If the array has 22 >elements does it go from 0 to 21 or 1 to 22? > > > -- Michael L. Crawley, CIO Business Strategy, E-Technology Specialist Jenai Communications P.O. Box 33 Wheeling, Illionis 60090 (847) 745 - 0940 http://www.jenaipower.net mailto:[EMAIL PROTECTED] --
Re: foreach examples/usage
link works.. but it's on a free server (sharemation) so sometimes the server is down... i should have my own up shortly, at japh.nu... just have to fiddle with the dns entry... =) Jos - Original Message - From: "mcrawley " <[EMAIL PROTECTED]> To: "Evan Panagiotopoulos" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>; "Jos I. Boumans" <[EMAIL PROTECTED]> Sent: Sunday, July 08, 2001 4:39 PM Subject: Re: foreach examples/usage > Sounds interesting. Please verify this link. > -- Original Message -- > From: "Jos I. Boumans" <[EMAIL PROTECTED]> > Date: Sun, 8 Jul 2001 11:19:21 +0200 > > > > >you might find the loops/block tutorial i wrote on > >www.sharemation.com/~perl/tut helpfull > > > >regards, > > > >Jos Boumans > > > > > >I am trying to learn the foreach loop. Is there a place on the web where > >there are examples for the foreach? Basically I have an array of X elements > >and I want to go from the first element (is it zero?) to the last. I have a > >variable containing the number of elements in the array. If the array has 22 > >elements does it go from 0 to 21 or 1 to 22? > > > > > > > > -- > Michael L. Crawley, CIO > Business Strategy, E-Technology Specialist > Jenai Communications > P.O. Box 33 > Wheeling, Illionis 60090 > (847) 745 - 0940 > http://www.jenaipower.net > mailto:[EMAIL PROTECTED] > > -- >
Re: foreach examples/usage
you might find the loops/block tutorial i wrote on www.sharemation.com/~perl/tut helpfull regards, Jos Boumans I am trying to learn the foreach loop. Is there a place on the web where there are examples for the foreach? Basically I have an array of X elements and I want to go from the first element (is it zero?) to the last. I have a variable containing the number of elements in the array. If the array has 22 elements does it go from 0 to 21 or 1 to 22?
Re: foreach examples/usage
Evan wrote: > I am trying to learn the foreach loop. Is there a > place on the web where there are examples for the > foreach? There's good, quick example on Perl Monks: http://www.perlmonks.org/index.pl?node=foreach%20loops&lastnode_id=954 > Basically I have an array of X elements and > I want to go from the first element (is it zero?) to > the last. I have a variable containing the number of > elements in the array. If the array has 22 elements > does it go from 0 to 21 or 1 to 22? It goes from 0 to 21. It sounds like you're just starting to learn Perl. I'd suggest checking out http://learn.perl.org and purchasing one of their book suggestions. = Dave Hoover "Twice blessed is help unlooked for." --Tolkien http://www.redsquirreldesign.com/dave __ Do You Yahoo!? Get personalized email addresses from Yahoo! Mail http://personal.mail.yahoo.com/
RE: foreach examples/usage
The first place in your array is 0, There are a couple of examples of foreach that I don't mind giving: my @array = qw{this is the example of the array we will use for this example}; foreach(@array) { print $_."\n"; # current element is $_ } or if you need the index of the array: foreach (0..$#array) { print $array[$_]."\n"; # now $_ is current of the count in foreach } or you can just go print $_ foreach (0..100); or the earlier examples: print "$_\n" foreach (@array); Does that help? Steve H. -Original Message- From: Evan Panagiotopoulos [mailto:[EMAIL PROTECTED]] Sent: Tuesday, July 17, 2001 8:47 PM To: [EMAIL PROTECTED] Subject: foreach examples/usage I am trying to learn the foreach loop. Is there a place on the web where there are examples for the foreach? Basically I have an array of X elements and I want to go from the first element (is it zero?) to the last. I have a variable containing the number of elements in the array. If the array has 22 elements does it go from 0 to 21 or 1 to 22? Thanks, Evan Panagiotopoulos Technology, Library and Media Director Poughkeepsie City School District Yes We Can! Watch Us Do It!
foreach examples/usage
I am trying to learn the foreach loop. Is there a place on the web where there are examples for the foreach? Basically I have an array of X elements and I want to go from the first element (is it zero?) to the last. I have a variable containing the number of elements in the array. If the array has 22 elements does it go from 0 to 21 or 1 to 22? Thanks, Evan Panagiotopoulos Technology, Library and Media Director Poughkeepsie City School District Yes We Can! Watch Us Do It!