Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: How to make references more like aliases?

by Limbic~Region (Chancellor)
on Sep 28, 2004 at 00:52 UTC ( [id://394423]=note: print w/replies, xml ) Need Help??


in reply to Re: How to make references more like aliases?
in thread How to make references more like aliases?

blokhead,
I can certainly live with $group->[ <index> ] as it is much prettier. Thanks a bunch!
#!/usr/bin/perl use strict; use warnings; my @array = 0 .. 30; my $next = by_groups_of( 3, \@array ); while ( my $group = $next->() ) { $group->[1] = 'foo'; } print "$_\n" for @array; sub by_groups_of { my ($by, $list) = @_; return sub { () } if ! $by || $by =~ /\D/ || ! @$list; my $pos = 0; my $done; return sub { return () if $done || $pos > $#$list; my $start = $pos; my $stop = $pos + $by - 1 > $#$list ? $#$list : $pos + $by - 1 +; $pos += $by; $done = 1 if $stop == $#$list; return sub { \@_ }->( @$list[ $start .. $stop ] ); } }

Cheers - L~R

Update: Modified code here to reflect all the recommendations and bug fixes

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-25 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found