Re: Keeping a Count in foreach
by robobunny (Friar) on Jun 17, 2002 at 15:32 UTC
|
if you don't want to use a counter, how about:
foreach $index (0..$#s) {
print "$index: $s[$index]\n";
}
| [reply] [d/l] |
Re: Keeping a Count in foreach
by insensate (Hermit) on Jun 17, 2002 at 15:30 UTC
|
$i=0;
foreach $e (@s) {
print $i++." $e\n";
}
suffice? If not, could you clarify your intent?
Thanks, Jason | [reply] [d/l] |
Re: Keeping a Count in foreach
by jmcnamara (Monsignor) on Jun 17, 2002 at 16:01 UTC
|
| [reply] |
Re: Keeping a Count in foreach
by DamnDirtyApe (Curate) on Jun 17, 2002 at 17:29 UTC
|
Arun;
If you don't want your counter to stick around after you're done looping through you list, use a bare block to limit the scope to your loop plus the counter declaration.
#! /usr/bin/perl
use strict ;
my @s = qw/ foo bar foobar FOO BAR FOOBAR / ;
{
my $count = 0 ;
foreach my $e ( @s )
{
print $count++, ": $e\n" ;
}
}
# $count is no longer in scope.
_______________
D
a
m
n
D
i
r
t
y
A
p
e
Home Node
|
Email
| [reply] [d/l] |
Re: Keeping a Count in iforeach/i
by neilwatson (Priest) on Jun 17, 2002 at 15:26 UTC
|
my $count=0;
foreach $e (@s) {
$count ++;
print "$e is the $count"."th element\n";
}
Neil Watson
watson-wilson.ca | [reply] [d/l] |
Re: Keeping a Count in foreach
by I0 (Priest) on Jun 17, 2002 at 18:27 UTC
|
foreach $e (@s) {
print(($.||1..$.&&0)-1," $e\n");
}
| [reply] [d/l] |
|
if you're going to obfuscate it, why not
map print(($.||1..$.&&0)-1," $_$/"),@s;
and be done with it? {shudder}
~Particle *accelerates*
| [reply] [d/l] [select] |
A reply falls below the community's threshold of quality. You may see it by logging in.
|
|
Hi can you please explain this syntax ($.||1..$.&&0)-1 , i tried to get it but i am unable to understand it.
| [reply] |
|
Hi can you please explain this syntax|1..$.&&0)-1 , i tried to get it but i am unable to understand it.
| [reply] |
Re: Keeping a Count in foreach
by mfriedman (Monk) on Jun 17, 2002 at 18:03 UTC
|
Can I do this using the foreach structure easily or will I have to resort to a for loop?
There is no real difference between for and foreach, except for convention. From perlsyn:
The foreach keyword is actually a synonym for the for keyword, so you can use foreach for readability or for for brevity.
So,
perl -e 'foreach my $dir(@INC) { print "$dir\n"; }'
perl -e 'for my $dir(@INC) { print $dir\n"; }'
Are exactly the same, as are
perl -e 'foreach($i = 0; $i < 10; $i++) { print "$i\n"; }'
perl -e 'for($i = 0; $i < 10; $i++) { print "$i\n"; }'
| [reply] [d/l] [select] |
|
Yes, but the first two are completely different from the last two. We have to call these control structures *something*. When talking about them, we call the first two "foreach loops", and the last two "for loops", regardless of which keyword is used. perlsyn follows this convention in its =head2 headers, and arunhorne is using it in his question.
$ perl -MO=Deparse,p -e 'for(@z){print}'
foreach $_ (@z) {
print $_;
}
-e syntax OK
| [reply] [d/l] [select] |
|
| [reply] |