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.


In reply to Re^5: Using an array element as a loop iterator by AnomalousMonk
in thread Using an array element as a loop iterator by gurpreetsingh13

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.