Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Printing from three arrays

by johngg (Canon)
on Sep 26, 2019 at 14:29 UTC ( [id://11106753]=note: print w/replies, xml ) Need Help??


in reply to Printing from three arrays

Another alternative to looping is to use the glob built-in.

johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -E ' my @arr1 = qw{ 1 2 3 }; my @arr2 = q{a} .. q{g}; my @arr3 = qw{ I II III }; my $globStr = qq{{@{ [ join q{,}, @arr1 ] }}} . qq{{@{ [ join q{,}, @arr2 ] }}} . qq{{@{ [ join q{,}, @arr3 ] }}}; say $globStr; say for glob $globStr;' {1,2,3}{a,b,c,d,e,f,g}{I,II,III} 1aI 1aII 1aIII 1bI 1bII 1bIII 1cI 1cII 1cIII 1dI 1dII 1dIII 1eI 1eII 1eIII 1fI 1fII 1fIII 1gI 1gII 1gIII 2aI 2aII 2aIII 2bI 2bII 2bIII 2cI 2cII 2cIII 2dI 2dII 2dIII 2eI 2eII 2eIII 2fI 2fII 2fIII 2gI 2gII 2gIII 3aI 3aII 3aIII 3bI 3bII 3bIII 3cI 3cII 3cIII 3dI 3dII 3dIII 3eI 3eII 3eIII 3fI 3fII 3fIII 3gI 3gII 3gIII

I hope this is of interest.

Update : AnomalousMonk quite rightly points out that I'm the idiot who didn't read the question properly, a failing that has dogged me since O- and A-levels at school :-(

Getting the left-hand value to circulate the fastest and inserting the required commas involves some jiggery-pokery to insert marker text into a re-ordered $globStr and a split, reverse and join in a map for each string generated by glob. Probably more trouble than it's worth.

johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -E ' my @arr1 = qw{ 1 2 3 }; my @arr2 = q{a} .. q{g}; my @arr3 = qw{ I II III }; my $globStr = qq{{@{ [ join q{,}, @arr3 ] }}} . q{__} . qq{{@{ [ join q{,}, @arr2 ] }}} . q{__} . qq{{@{ [ join q{,}, @arr1 ] }}}; say $globStr; say for map { join q{,}, reverse split m{__} } glob $globStr;' {I,II,III}__{a,b,c,d,e,f,g}__{1,2,3} 1,a,I 2,a,I 3,a,I 1,b,I 2,b,I 3,b,I 1,c,I 2,c,I 3,c,I 1,d,I 2,d,I 3,d,I 1,e,I 2,e,I 3,e,I 1,f,I 2,f,I 3,f,I 1,g,I 2,g,I 3,g,I 1,a,II 2,a,II 3,a,II 1,b,II 2,b,II 3,b,II 1,c,II 2,c,II 3,c,II 1,d,II 2,d,II 3,d,II 1,e,II 2,e,II 3,e,II 1,f,II 2,f,II 3,f,II 1,g,II 2,g,II 3,g,II 1,a,III 2,a,III 3,a,III 1,b,III 2,b,III 3,b,III 1,c,III 2,c,III 3,c,III 1,d,III 2,d,III 3,d,III 1,e,III 2,e,III 3,e,III 1,f,III 2,f,III 3,f,III 1,g,III 2,g,III 3,g,III

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Printing from three arrays
by AnomalousMonk (Archbishop) on Sep 26, 2019 at 18:20 UTC

    IIRC, glob iterates most slowly over its first (leftmost) iteration group,  {1,2,3} in this case, and more rapidly over each succeeding iteration group, so that the the last (rightmost) group  {I,II,III} iterates most quickly. This is the behavior I see in the output of your example code: the first (leftmost, arabic-number) field or column of each  "1aI" output record/string changes most slowly, the second (middle, alpha) changes more quickly, and the third (rightmost, roman-number) column changes fastest.

    However, oysterperl, as shown in the example of desired output in the OP, seems to want the first (leftmost, arabic-number) column to change most quickly, the second (alpha) column to change most slowly, and the third (roman-number) column to change at an intermediate rate. Is it possible to produce this result with a glob solution? (The OPed example output also has commas separating the columns, but this is easily done with a glob template.)


    Give a man a fish:  <%-{-{-{-<

      I checked the glob page, and I could not find something that would make it change it's ways, so you would need to split and join it :(

      perl -e '@_=split(/=/, $_) and print join(", " ,@_[2,0,1]),"\n" for glob "{a,b,c}={I,II,III}={1,2,3,4}"'

      it's cumbersome, though. I tried something with unshift pop but it was longer.

      perl -E '@_=split /=/ and unshift(@_,pop @_) and say join ", ", @_ for glob "{a,b,c}={I,II,III}={1,2,3,4}"'

      perl -E '@_=split /=/ and say join ", ", unshift(@_, pop @_) && @_ for glob "{a,b,c}={I,II,III}={1,2,3,4}"'

        Not really much (if any) better:

        c:\@Work\Perl\monks>perl -wMstrict -le "use 5.014; ;; print s{\A(.+?),(.+?),(.+?)}{$3,$1,$2}xmsr for glob '{a,b,c},{I,II,III},{1,2,3,4}'; "
        (I won't show the output; it's as expected.)


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-19 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found