Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Automatic Loop Counter not in perl

by Roy Johnson (Monsignor)
on Aug 21, 2007 at 17:21 UTC ( [id://634142]=note: print w/replies, xml ) Need Help??


in reply to Automatic Loop Counter not in perl

You've got some illegal syntax in %LoH->[$count]. But presuming I know what you meant, here's a nestable, localized-loop-counter-providing loop function that handles most of your wants, without* being too awkward:
use strict; use warnings; sub counted_for (&@) { local our $LOOPCOUNT=-1; my $sub = shift; for (@_) { ++$LOOPCOUNT; $sub->(); } } # Define a modestly complicated structure to loop through my $LoH; my $count = 3; @{$LoH->[$count]}{'a'..'e'} = (); my @y = ('X'..'Z'); counted_for { print "$LOOPCOUNT: $_\n"; counted_for { print " $LOOPCOUNT $_\n" } keys %{$LoH->[$count]}; } @y;
*arguably; because prototypes require that non-explicit subs be the first arg, I can't duplicate the for syntax, and of course I can't make a predicate form. Maybe it should be a map replacement instead.

Update: And of course last and next won't work with it.will give you warnings.


Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found