Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

For and foreach...

by Elgon (Curate)
on Jan 20, 2001 at 20:14 UTC ( #53222=note: print w/replies, xml ) Need Help??

in reply to difference between for and foreach?

Perl has these two statements for a reason and they are appropriate each in different cases. When I first learned how to use foreach I was astounded how groovy and useful it can be.

The for command is great for performing a single series of mathematical calculations on a continous and possibly contiguous list of numbers, for example. I don't use it a great deal because what you used to do in BASIC with ...

for i = 1 to 10 myarray(i) = myarray(i) + 1 next i

Or equivalently in Perl...

my $i; my @array = (1, 5, 6, 9, 57); for ($i=0; $i <= (scalar(@array)-1); ++$i) { ++$array[$i]; } done far more easily (and efficiently?) with foreach.

foreach $bar(@bar) { ++$bar; }

Basically, foreach allows you to act over a data structure and perform a function efficiently, whereas for allow you to work over a fixed range of numbers with a function (and then presumably do something with the result) or perform a function a given number of times.

Okay, now for the lecture part: Finding this sort of information isn't too difficult, a good start would have been typing 'For loops' and 'Foreach loops' in the search box at the top left of most/all pages on Perlmonks. Don't sweat it, we've all done it at some point and I doubt anyone here will flame you to a crisp but try and give it a go yourself first, play with Perl. Ideally if you are having problems make the title descriptive and then post some example code (inside <code> </code> tags to make it purty) and ask for help: we promise we won't laugh, at least they haven't laughed at me yet!

See this tutorial and this tutorial for help.


Update: In response to enlightened posters such as salvadors and turnstep, I am aware of Perl's ability to do 'the right thing' and the fact that in many ways for and foreach are identical, however in it is easier to read and therefore generally 'better style' to use these two commands appropriately. The `efficiently?` comment should be read in backticks to invite answers by people who know more about how Perl works at the lower levels than me.

On a final note, thankyou to merlyn for explaining what I was trying to get across, only better.

Replies are listed 'Best First'.
Re: For and foreach...
by salvadors (Pilgrim) on Jan 20, 2001 at 21:28 UTC

    Perl has these two statements for a reason and they are appropriate each in different cases ... is done far more easily (and efficiently?) with foreach.

    No, no, no. For and foreach are identical. Yes, they're there for a reason, but that's not because they do anything different, or because one is better at one thing, or anything like that.

    Perl as a language grew out of linguistic principles. Larry describes Perl as a diagonal language rather than an orthogonal one. It has no problem having more than one command that does exactly the same thing, just as most natural languages have words that mean exactly the same thing. The two different versions of for make it easier to read Perl in English.

    You can quite happily, to take your examples, write:

    foreach ($i=0; $i <= (scalar(@array)-1); ++$i) { ++$array[$i]; }
    for $bar (@bar) { ++$bar; }
    and perl will Do The Right Thing(tm). In fact it will Do The Same Thing. But many people will find it easier to read them the original way. Which is important in the Perl World.


      and don't forget:
      for my $i (0..$#array) { ++$array[$i]; }
      for those times when you need the index
      much cleaner than doing it C-style.


      (the triplet paradiddle)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://53222]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2020-02-25 13:49 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (110 votes). Check out past polls.