in reply to Is there a Perl variable to count iterations in a loop?
There is probably no such variable, but you can play with a range operator .. in scalar context:
my @letters = qw(alfa beta gama); for my $letter (@letters){ warn "iteration is ", scalar($0 .. !$0); }
The $0, resp. !$0 are just expressions which are allways true, resp. allways false. Unfortunately you cannot use 1, 0 because they are given some special meaning by the range operator.
Although this "trick" works, it is not particularly self-explainable.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is there a Perl variable to count iterations in a loop?
by ruzam (Curate) on Dec 18, 2008 at 23:00 UTC | |
by roman (Monk) on Dec 19, 2008 at 10:50 UTC | |
by pat_mc (Pilgrim) on Dec 19, 2008 at 11:27 UTC | |
by LanX (Saint) on Dec 19, 2008 at 11:29 UTC |