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

How to warp an array by trading X/Y coordinates (rows/columns)

by Polyglot (Chaplain)
on Mar 08, 2016 at 15:29 UTC ( [id://1157088]=perlquestion: print w/replies, xml ) Need Help??

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

I'm pushing a large number of items into an array that is later converted into columns in LaTeX. LaTeX puts the items in order, vertically, spilling over into the next column after each column is filled. I need the items to be placed across the page in rows, instead of vertically. Owing to code that allows the column count to be adjustable, I cannot predict how many columns there will be, but I can use the user-variable for the column count to help transform the array. It seems like something like 'map' should be able to do this efficiently, but I have no idea how.

I could do this by brute force, of course, creating a separate array for each column, then shifting off the original array and shuffling to each column's array, then pushing them all into a single array again. But that just doesn't seem like the Perl way to do things. I couldn't find anything about this here or via Google--but it seems someone might have wanted to transform an array like this before. Your wisdom is welcome.

Essentially, I would like to convert something like this:

OneFiveNineThirteen
TwoSixTenFourteen
ThreeSevenElevenFifteen
FourEightTwelveSixteen

Into something like this:

OneTwoThreeFour
FiveSixSevenEight
NineTenElevenTwelve
ThirteenFourteenFifteenSixteen

Look like fun?

Blessings,

~Polyglot~

  • Comment on How to warp an array by trading X/Y coordinates (rows/columns)

Replies are listed 'Best First'.
Re: How to warp an array by trading X/Y coordinates (rows/columns)
by choroba (Cardinal) on Mar 08, 2016 at 15:51 UTC
    Note that for squares (as in your example), you can rotate the table in place:
    #!/usr/bin/perl use warnings; use strict; use Text::Table; my @tab = ( [qw[ One Five Nine Thirteen ]], [qw[ Two Six Ten Fourteen ]], [qw[ Three Seven Eleven Fifteen ]], [qw[ Four Eight Twelve Sixteen ]], ); print 'Text::Table'->new(@tab); for my $i (0 .. $#tab) { for my $j (0 .. $i - 1) { ( $tab[$i][$j], $tab[$j][$i] ) = ( $tab[$j][$i], $tab[$i][$j] +); } } print '-' x 20, "\n"; print 'Text::Table'->new(@tab);

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: How to warp an array by trading X/Y coordinates (rows/columns)
by BrowserUk (Patriarch) on Mar 08, 2016 at 15:39 UTC

    Like this?:

    #! perl -slw use strict; use Data::Dump qw[ pp ]; my @data = map[ split ' ' ], <DATA>; pp \@data; my @xformed; push @xformed, [ map shift @$_ // (), @data ] while @{ $data[ 0 ] }; pp \@xformed; __DATA__ 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16

    Output:

    C:\test>1157088.pl [[1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]] [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Wow, you both are fast! Thank you! I thought there must be a way to do this with map. I'm not working on single characters, but with split I think I can find a suitable delimiter. (In my case, I'm actually working with LaTeX codes for each entry, so each array "element" is actually a piece of code text.) I'll give your suggestion a try.

      Blessings,

      ~Polyglot~

        I'm not working on single characters, but with split I think I can find a suitable delimiter.

        I was just being lazy. It works exactly the same for strings provided they do not contain spaces:

        C:\test>1157088.pl [ ["one", "five", "nine", "thirteen"], ["two", "six", "ten", "fourteen"], ["three", "seven", "eleven", "fifteen"], ["four", "eight", "twelve", "sixteen"], ] [ ["one", "two", "three", "four"], ["five", "six", "seven", "eight"], ["nine", "ten", "eleven", "twelve"], ["thirteen", "fourteen", "fifteen", "sixteen"], ]

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to warp an array by trading X/Y coordinates (rows/columns)
by toolic (Bishop) on Mar 08, 2016 at 15:35 UTC
Re: How to warp an array by trading X/Y coordinates (rows/columns)
by VinsWorldcom (Prior) on Mar 08, 2016 at 17:20 UTC
Re: How to warp an array by trading X/Y coordinates (rows/columns)
by Your Mother (Archbishop) on Mar 08, 2016 at 17:27 UTC
Re: How to warp an array by trading X/Y coordinates (rows/columns)
by jcb (Parson) on Mar 09, 2016 at 00:44 UTC

    If I read the text of your post correctly, you actually have a one-dimensional array and others have been confused by the output samples that represent LaTeX output. Here's a solution that avoids multi-dimensional arrays:

    #!/usr/bin/perl use strict; use warnings; sub pa { print join(', ', @_), "\n" } die "usage: $0 <number of items> <number of columns>" unless $#ARGV == 1; my @IN = 1 .. $ARGV[0]; my $COLS = $ARGV[1]; my @OUT = (); pa @IN; for my $i (0 .. $COLS-1) { for (my $j = $i; $j<=$#IN; $j+=$COLS) { push @OUT, $IN[$j] } push @OUT, "{}" } pa @OUT;

    Since you are making LaTeX output, each {} in the output marks a column break. Substitute whatever command you need to produce a column break for "{}". This should work for any number of data items and any number of columns.

    Update: If you would rather have blank items as placeholders than explicit column breaks, you can change the loop to:

    for my $i (0 .. $COLS-1) { push @OUT, @IN[map {$_*$COLS+$i} 0..($#IN/$COLS)]; }

    This will leave undef elements in @OUT for each table cell that should be blank. I had initially overlooked that array slices need not be contiguous in Perl and requesting an element off the end of an array produces undef.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-19 13:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found