Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: convert rows to columns

by Athanasius (Chancellor)
on Nov 26, 2012 at 12:53 UTC ( #1005637=note: print w/replies, xml ) Need Help??

in reply to convert rows to columns

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

Replies are listed 'Best First'.
Re^2: convert rows to columns
by ack (Deacon) on Nov 26, 2012 at 20:30 UTC

    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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005637]
[Discipulus]: too much guns in schools? throw more gun at schools! professors in arms
LanX thinks teachers nowadays need every possible mean to defend against their annoying students.

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-02-23 11:38 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (302 votes). Check out past polls.