Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The following are all implemented differently:

  • for (EXPR; EXPR; EXPR) ("C-style for loop", an augmented while loop.)
  • for (EXPRX..EXPRY) (A range and nothing else.)
  • for (reverse CONSTX..CONSTY) (A constant range, preceded by reverse.)
  • for (reverse EXPRX..EXPRY) (A variable range, preceded by reverse.)
  • for (@ARRAY) (An array and nothing else.)
  • for (reverse @ARRAY) (Reverse of an array and nothing else.)
  • for (reverse LIST) (Reverse of any list that doesn't fit the above patterns.)
  • for (LIST) (Any list that doesn't fit the above patterns.)

You might find the difference between foreach (@ARRAY) and foreach (LIST) interesting.

{ my $x = 1; my $y = 4; # The initial values are saved. my @a; foreach ($x..$y) { push @a, $_; $y++; } print("@a\n"); # 1 2 3 4 } { my $x = 1; my $y = 4; # The initial values are saved. my @a; foreach (reverse $x..$y) { push @a, $_; $x--; } print("@a\n"); # 4 3 2 1 } { my @a = (1, 2, 3, 4); my $i = 5; # Loops "while (pass_num < @a)". # In this case, that means loop forever. foreach (@a) { # Loops "while (pass_num < @a)" push(@a, $i++); if (@a == 20) { push(@a, '...'); last; } # Avoid infinite loop. } print("@a\n"); # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ... } { my @a = (1, 2); my @b = (3, 4); my $i = 5; # Creates a list at the start of the # loop and iterates over that list. # In this case, elements are added to @b, # but not to the list on the stack, so # it loops 4 times. foreach (@a, @b) { push(@b, $i++); } print("@a @b\n"); # 1 2 3 4 5 6 7 8 }

The difference between
foreach (reverse CONSTX..CONSTY)
and
foreach (reverse EXPRX..EXPRY)
is that the list in built at compile time in the former.

>perl -le "for (1..2) { for (reverse 1..3) { print; $_=5; } }" 3 2 1 5 5 5 >perl -le "for (1..2) { for (reverse 1..($x=3)) { print; $_=5; } }" 3 2 1 3 2 1

In reply to Re^3: Mutable foreach list? (types of for) by ikegami
in thread Mutable foreach list? by zigdon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-28 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found