tel2 has asked for the wisdom of the Perl Monks concerning the following question:
Why is it that this runs:
$ perl -e 'for $a (1..2){for $b (10..12){print "$a-$b\n"}}'
1-10
1-11
1-12
2-10
2-11
2-12
and this runs:
$ perl -e 'for $a (1..2){print "$a-$_\n" for (10..12)}'
1-10
1-11
1-12
2-10
2-11
2-12
but this crashes with a syntax error:
$ perl -e 'for $a (1..2){print "$a-$b\n" for $b (10..20)}'
syntax error at -e line 1, near "$b ("
Execution of -e aborted due to compilation errors.
in Perl v5.10.1.
Thanks for your time!
Tel2
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Syntax error with nested for loop
by Athanasius (Archbishop) on Sep 22, 2015 at 07:41 UTC | |
by tel2 (Pilgrim) on Sep 22, 2015 at 20:59 UTC | |
|
Re: Syntax error with nested for loop
by Myrddin Wyllt (Hermit) on Sep 22, 2015 at 13:03 UTC | |
by tel2 (Pilgrim) on Sep 22, 2015 at 21:04 UTC | |
|
Re: Syntax error with nested for loop
by u65 (Chaplain) on Sep 22, 2015 at 22:18 UTC |