Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^2: Automatic Loop Counter not in perl

by liverpole (Monsignor)
on Aug 21, 2007 at 13:33 UTC ( #634080=note: print w/replies, xml ) Need Help??

in reply to Re: Automatic Loop Counter not in perl
in thread Automatic Loop Counter not in perl

++b4swine for a nice meditation, and ++roboticus for further expanding on the idea.

Many have been the times I've gone from having a simple foreach loop to a for loop, just because I realized that the indices would be required.

For example, let's say I started with a straightforward:

#!/usr/bin/perl -w use strict; use warnings; my @array = qw( a b c d e f g h ); # Code sample 1 -- for simplicity, just display each array item: foreach (@array) { printf "%s\n", $_; }

Then I realize that I need the index each time through the loop, for whatever reason.  Again, for simplicity's sake, let's assume the index is only needed for display purposes.  Even so, it requires changing from a foreach loop to a for loop, and adding a line to set some variable to the next item in the list each time:

# Code sample 2 -- Come to think of it, I want the indices too ... for (my $i = 0; $i < @array; $i++) { my $item = $array[$i]; printf "%3d. $item\n", $i + 1; }

But wouldn't it be nice if there were a default variable set to the index?  Then I could make minimal changes to the original foreach loop:

# Code sample 3 -- minor changes from the original "foreach" # loop (Code sample 1) foreach (@array) { printf "%3d. %s\n", $loopcount + 1, $_; }

I like roboticus' idea of having an array or hash to hold multiple levels of the variable.  Ideally, there would be both, a scalar variable which holds the index of the current block, and an array or hash holding the indices from all loops.

The only thing I would suggest to do differently is find a special variable to replace $loopcount.  An obvious choice might be $#, which is already deprecated in its current use --   from perlvar:

$# The output format for printed numbers. This variable is a half-hearted attempt to emulate awk's OFMT variable. There are times, however, when awk and Perl have differing notions of what counts as numeric. The initial value is "", where n is the value of the macro DBL_DIG from your systemís float.h. This is different from awk's default OFMT setting of "%.6g", so you need to set $# explicitly to get awk's value. (Mnemonic: # is the number sign.) Use of $# is deprecated.


Replies are listed 'Best First'.
Re^3: Automatic Loop Counter not in perl
by wfsp (Abbot) on Aug 21, 2007 at 13:41 UTC
    I'd do it like this
    my @array = qw{zero one two}; for my $i (0..$#array){ print qq{$i\t$array[$i]\n}; }
      And if you really wanted $_:
      my @array = qw{zero one two}; for my $i (0..$#array){ for ($array[$i]) { print qq{$i\t$_\n}; } }
Re^3: Automatic Loop Counter not in perl
by twotone (Beadle) on Aug 15, 2008 at 20:47 UTC

    So, if


    is depreciated. Is there something equivalent to use?

    Have a nice day :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://634080]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2020-01-22 03:22 GMT
Find Nodes?
    Voting Booth?