Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Perl nested loop to print out two arrays n number of times in different patterns

by arty32l (Initiate)
on Sep 23, 2014 at 00:26 UTC ( [id://1101585]=perlquestion: print w/replies, xml ) Need Help??

arty32l has asked for the wisdom of the Perl Monks concerning the following question:

I am looking to create a loop where the following data is printed n number of times. So given:"

n = 9; @a = (1, 2 ,3); @b = (a, b, c);
$a[0] = 1 $b[0] = a $a[0] = 1 $b[1] = b $a[1] = 2 $b[2] = c $a[1] = 2 $b[0] = a $a[2] = 3 $b[1] = b $a[2] = 3 $b[2] = c $a[0] = 1 $b[0] = a $a[0] = 1 $b[1] = b $a[1] = 2 $b[2] = c

Replies are listed 'Best First'.
Re: Perl nested loop to print out two arrays n number of times in different patterns
by biohisham (Priest) on Sep 23, 2014 at 02:51 UTC

    Read How do I post a question effectively? if you want good answers. We really encourage people who show they are trying to learn. Posting what seems to be a homework question without even showing an attempt to solve it and also providing an inconsistent example of the required output sets a lot of red-flags that will make many monks refrain from answering. If you want good answers you post good questions.

    With that said, the following code gets closer to what you want, you can expand on it by holding the index values for each array at any given iteration and reporting that. This is left as an exercise to the OP.

    my @numbers=(1,2,3); my @letters=("a","b","c"); for(0..$#numbers){ my $arr_curr_val= $numbers[$_]; for(0..$#letters){ print "$arr_curr_val\t$letters[$_]\n"; } }
    Update: gave the arrays meaningful names rather than @array1 and @array2. Thanks to Anonymous Monk who pointed that out.

    A 4 year old monk

      If instead of using array indexes you use the actual array values is saves the bother of having to save the current outer loop value. Anonymonk makes a useful point about meaningful variable names.

      $ perl -Mstrict -Mwarnings -E ' my @nums = qw{ 1 2 3 }; my @ltrs = qw{ a b c }; my $iters = 2; for my $iter ( 1 .. $iters ) { for my $num ( @nums ) { for my $ltr ( @ltrs ) { say qq{$num$ltr}; } } }' 1a 1b 1c 2a 2b 2c 3a 3b 3c 1a 1b 1c 2a 2b 2c 3a 3b 3c $

      Another way to do this would be to localise the list separator and use glob and the list multiplier in a do block.

      $ perl -Mstrict -Mwarnings -E ' my @nums = qw{ 1 2 3 }; my @ltrs = qw{ a b c }; my $iter = 2; say for do { local $" = q{,}; ( glob qq{{@nums}{@ltrs}} ) x $iter; };' 1a 1b 1c 2a 2b 2c 3a 3b 3c 1a 1b 1c 2a 2b 2c 3a 3b 3c $

      I hope this is of interest.

      Cheers,

      JohnGG

      ... good advice ... @array1 ... @array2 ...

      Hi :) @numbers and @letters :)

Re: Perl nested loop to print out two arrays n number of times in different patterns
by LanX (Saint) on Sep 23, 2014 at 01:21 UTC
    Your homework data doesn't make sense.

    For instance where is  $a[0] = 1 $b[2] = c ?

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: Perl nested loop to print out two arrays n number of times in different patterns
by Anonymous Monk on Sep 23, 2014 at 00:43 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-19 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found