Depending on how often you need to use this construct, you could build your own iterator with callback, to handle this:
my @arr = ( 110..117) ;
# Generic array Iterator ---
# Calls back $cb passing it a 2-element array : the INDEX, and an alia
+s to the contents at that index
sub iter{
my ($cb,$aref)=@_; # $cb is the callback sub ref passed in..
my $idx=0;
for(@$aref){
$cb->($idx,$_);
$idx++
}
};
# Call the iterator
iter(sub{print "ops : @_\n"},\@arr);
# Prints:
#ops : 0 110
#ops : 1 111
#ops : 2 112
# ....etc ...
Advantages:
- Index is localized/encapsulated (and can be hardened, if necessary)
- No temporary arrays created (Memory efficient)
- CPU - Efficient (minimal overhead)
- Update 1:No initialization required, No overlap or threading issues(As compared to other implementations of the "each" function)
"As you get older three things happen. The first is your memory goes, and I can't remember the other two... "
- Sir Norman Wisdom
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.