Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^5: Using an array element as a loop iterator

by Eily (Curate)
on Nov 08, 2013 at 17:14 UTC ( #1061733=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Using an array element as a loop iterator
in thread Using an array element as a loop iterator

Oh, it's far clearer with the nested loops example ^^. You could also do:

for(@a1=`cat file`;$a[0]=shift @a1;@a1) { for(@a2=@b;$a[1]=shift @a2;@a2) # could be "for(;$a[1]=shift @b;@b)" + if you don't care about emptying @b { for(@a3=grep /4/, @c;$a[2]=shift @a3;@a3) { <Statements>; } } }
but that's kind of ugly. Maybe you could use that with source filtering to have a lighter syntax ...


If you intend to do that sort of things often in your script you could use Perl Prototypes to add that kind of syntax to your program:

sub with {for (@{$_[1]}){$_[0] = $_;$_[2]();}} sub in { \@_ } sub run(&) { $_[0] }
Then you could write :
# parenthesis mandatory after in, bad idea after with and the commas c +an't be ommited with $a[0], in(qw/Bonjour Bonsoir/), run { with $a[1], in ("Paul", "Jack", "Lord Voldemort"), run { say "@a"; }; # semi-colon mandatory }; # same here
Bonjour Paul Bonjour Jack Bonjour Lord Voldemort Bonsoir Paul Bonsoir Jack Bonsoir Lord Voldemort
But I did that because it was fun, and a bit of a challenge, but that's not very practical. You probably have to stick with my first answer.


Comment on Re^5: Using an array element as a loop iterator
Select or Download Code
Replies are listed 'Best First'.
Re^6: Using an array element as a loop iterator
by LanX (Canon) on Nov 08, 2013 at 19:45 UTC
    > but that's kind of ugly.

    maybe easier written with while loops?

     while ($a[0] =shift @a1) { ... }

    ... well at least for me better readable :)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      It surely is ^^. The problem is that you can't use that construct like this: while( $a[0] = shift `cat file` ) (but that is a unusual way to read a file, indeed). So you do have to set @a1 first, which is the first part of my for loop. And if @a1 contains a value that is false, $a[0] = shift @a1 will be false as well, so the loop will stop early. defined($a[0] = shit @a1) would work, unless you want to use undef as a value. That's why I used @a1 in scalar context as the third part of the for loop.

        agreed! ++ =)

        Cheers Rolf

        ( addicted to the Perl Programming Language)

      Thanks a lot. Got the point.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2015-07-30 23:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls