Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Fence vs. Posts

by hdb (Monsignor)
on Jun 08, 2018 at 11:41 UTC ( #1216189=note: print w/replies, xml ) Need Help??


in reply to Fence vs. Posts

Here is another complicated "perlish" solution to avoid simple C-style loops as proposed above (and which is the most sensible approach IMHO), this time using closures:

se warnings; use strict; sub create_scheduler { my $list = shift; return sub { my $i = shift; return () if $i > $#$list - 1 or $i < 0; return ( $list->[$i], $list->[$i+1] ); } }; my $leg = create_scheduler [ "Chicago", "Saint Looey", "Joplin", "OKC" +, "Amarillo", "Gallup", "Flagstaff", "Winona", "Kingman", "Barstow", +"San Bernandino", "LA" ]; my $i = 0; while( my @pair = $leg->($i++) ) { print join( " to ", @pair ), "\n"; }

You could also "enclose" the index into the closure if you want to create a one-time iterator over your array.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-05-17 18:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (68 votes). Check out past polls.

    Notices?