Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

convert rows to columns

by b00mIR (Initiate)
on Nov 26, 2012 at 12:35 UTC ( #1005630=perlquestion: print w/ replies, xml ) Need Help??
b00mIR has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to convert a file in the following format
abcdef abcdef abcdef abcdef to abababab cdcdcdcd efefefef
it's not a simiple conversion of rows to columns which i'm able to do but taking every pair of elements of a row and turning them into columns I've tried the following without success
foreach $line (@array) { chomp $line; @xox=split(//,$line); $cnt=0; for($i=0; $i<=scalar(@xox); $i+=2) { push $arr[$cnt], $xox[$i]."".$xox[1+$i]; $cnt++; } }

Comment on convert rows to columns
Select or Download Code
Replies are listed 'Best First'.
Re: convert rows to columns
by BrowserUk (Pope) on Nov 26, 2012 at 12:44 UTC
    #! perl -slw use strict; use Data::Dump qw[ pp ]; my @out; while( <DATA> ) { chomp; my $n = 0; $out[ $n++ ] .= $_ for unpack '(A2)*', $_; } pp \@out; __DATA__ abcdef abcdef abcdef abcdef


    C:\test> ["abababab", "cdcdcdcd", "efefefef"]

    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".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: convert rows to columns
by Athanasius (Canon) on Nov 26, 2012 at 12:53 UTC

    A variation on the theme:

    #! perl use Modern::Perl; use Data::Dump; my @array; while (<DATA>) { chomp; my @pairs = /(..)/g; $array[$_] .= $pairs[$_] for 0 .. $#pairs; } dd @array; __DATA__ abcdef abcdef abcdef abcdef


    22:46 >perl ("abababab", "cdcdcdcd", "efefefef") 22:50 >

    Hope that helps,

    Athanasius <°(((><contra mundum

      I assume in the second line of your "while" loop that you meant @pairs =~ /(..)/g; right?

      ack Albuquerque, NM

        No, I did mean =, for assignment.

        On each iteration of the while loop, the default variable $_ is implicitly assigned the next line read from the DATA handle. This $_ variable becomes the implicit argument to chomp, and then it’s implicitly bound to the regex match.

        So, the code I gave is equivalent to this:

        while ($_ = <DATA>) { chomp $_; my @pairs = $_ =~ /(..)/g; ...

        but more compact and “Perlish”. See perlvar#General-Variables.

        Hope that helps,

        Athanasius <°(((><contra mundum

Re: convert rows to columns
by McA (Priest) on Nov 26, 2012 at 12:47 UTC


    there is something which jumped into my eye: The index of an array runs from 0 to scalar(@array) - 1, so you have to adjust the condition of your for loop.

    Best regards

      I saw that, to. Also, you can't push to a sigle element of an array that I'm aware of. You have to push to an array. So the line push $arr[$cnt], $xox[$i]."".$xox[1+$i] I believe won't work. You need something like push @arr, $xox[$i]."".$xox[1+$i].

      ack Albuquerque, NM
        In recent Perl (since 5.14), you can push to an array reference. It is not autovivified, though; therefore,
        perl -e '$x[0] = []; push $x[0], 1'
        works, but
        perl -e 'push $x[0], 1'
        does not.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: convert rows to columns
by Anonymous Monk on Nov 26, 2012 at 13:24 UTC

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2015-11-27 04:45 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (718 votes), past polls