Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by Eily (Deacon)
on Nov 08, 2013 at 10:21 UTC ( #1061696=note: print w/ replies, xml ) Need Help??


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

That prints all four elements of b

10 11 12 13
and then a contains : [1, 13, 3]. So it does not correspond to your C-style for loop where you only print the first three elements, and the second value in the declaration of a is just thrown away.

To achieve the same result in Perl you can do:

@a = 1..3; @b = qw/2 4 7 9/; for (@b) {$a[1] = $_; say; }


Comment on Re^3: Using an array element as a loop iterator
Select or Download Code
Re^4: Using an array element as a loop iterator
by gurpreetsingh13 (Scribe) on Nov 08, 2013 at 15:17 UTC

    Thanks. That is nice

    for (@b){$a[1]=$_;say:}

    But what I actually wanted to achieve was that I had several nested foreach loops and taking several variables, one for each iterator, is nearly horrible. I wanted to initialize one array and then use that as a container for all the iterators of nested arrays.

    This step $a[1]=$_ or something similar will do that, but still that statement needs to be put as first step in loops of all 20 nested arrays. Isn't there any way through which we can avoid that and achieve the result via the foreach step only like something --

    my @a=(); foreach $a[0](`cat file`){ foreach $a[1](@b){ foreach $a[2](grep (/4/, @c)){ <More statements> }}}

    And not using something like--

    my @a=(); foreach (`cat file`){ $a[0]=$_; foreach $a[1](@b){ $a[1]$_; foreach $a[2](grep (/4/, @c)){ $a[2]=$_ <More statements> }}}
    </code>

      It sounds as if you might want something like Algorithm::Loops::NestedLoops (among many similar on CPAN):

      >perl -wMstrict -le "my $number = 99; my $letter = 'Z'; my $word = 'zot'; ;; my @words= qw(foo bar); ;; for my $number (1 .. 3) { for my $letter (qw(A B C)) { for my $word (@words) { do_something_with($number, $letter, $word); } } } ;; print qq{post-loop: '$number' '$letter' '$word' \n} ;; sub do_something_with { my (@args) = @_; ;; print qq{@args}; } ;; ;; use Algorithm::Loops qw(NestedLoops); ;; print qq{using NestedLoops:}; NestedLoops [ [ 1 .. 3 ], [ qw(A B C) ], \@words, ], \&do_something_with ; " 1 A foo 1 A bar 1 B foo 1 B bar 1 C foo 1 C bar 2 A foo 2 A bar 2 B foo 2 B bar 2 C foo 2 C bar 3 A foo 3 A bar 3 B foo 3 B bar 3 C foo 3 C bar post-loop: '99' 'Z' 'zot' using NestedLoops: 1 A foo 1 A bar 1 B foo 1 B bar 1 C foo 1 C bar 2 A foo 2 A bar 2 B foo 2 B bar 2 C foo 2 C bar 3 A foo 3 A bar 3 B foo 3 B bar 3 C foo 3 C bar

      With this approach it's easy to add arrays to arbitrary depth, albeit with attendant exponential explosion!

      Update: Changed example code to show  \@words taking reference to array.

      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.

        > 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)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-12-26 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (169 votes), past polls