Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Keeping a Count in foreach

by arunhorne (Pilgrim)
on Jun 17, 2002 at 15:21 UTC ( #175101=perlquestion: print w/ replies, xml ) Need Help??
arunhorne has asked for the wisdom of the Perl Monks concerning the following question:

Monks...

If i have the following:

foreach $e (@s) { print "$e\n"; }

Obviously this just prints the scalars in @s one per line. However I want to know what number $e is in a list i.e to print:

0 an_item 1 another_item etc.

Can I do this using the foreach structure easily or will I have to resort to a for loop?

Thanks,

</code> ____________
Arun

Comment on Keeping a Count in foreach
Select or Download Code
Re: Keeping a Count in iforeach/i
by neilwatson (Curate) on Jun 17, 2002 at 15:26 UTC
    You could use a counter:

    my $count=0; foreach $e (@s) { $count ++; print "$e is the $count"."th element\n"; }

    Neil Watson
    watson-wilson.ca

Re: Keeping a Count in foreach
by insensate (Hermit) on Jun 17, 2002 at 15:30 UTC
    Would something like:
    $i=0; foreach $e (@s) { print $i++." $e\n"; }
    suffice? If not, could you clarify your intent?
    Thanks, Jason
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"; }
Re: Keeping a Count in foreach
by jmcnamara (Monsignor) on Jun 17, 2002 at 16:01 UTC
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
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"; }'
      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
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"); }
      if you're going to obfuscate it, why not

      map print(($.||1..$.&&0)-1," $_$/"),@s;
      and be done with it? {shudder}

      ~Particle *accelerates*

        10pts for using map in a void context!


        (loses 2000 xp)
      Hi can you please explain this syntax|1..$.&&0)-1 , i tried to get it but i am unable to understand it.
      Hi can you please explain this syntax ($.||1..$.&&0)-1 , i tried to get it but i am unable to understand it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://175101]
Approved by coreolyn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-09-30 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (386 votes), past polls