Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: How to swap rows with columns?

by mwah (Hermit)
on Oct 09, 2007 at 21:16 UTC ( #643803=note: print w/replies, xml ) Need Help??

in reply to How to swap rows with columns?

Aside from the modules and the compact solutions,
I'll try a non-obfuscated solution first ;-) This will
work on any 'text-matrix' as far as it's rectangular ...
my $inp=' 1 2 3 4 5 6 a b c x y z '; my @data; my @elem = grep length, split /\s+/, $inp; my $rows = () = $inp =~/(?<=^)\w/msg; my $cols = @elem / $rows; for my $col (0..$cols-1) { push @data, [ map $elem[$col + $cols*$_], 0..$rows-1 ] } # print the matrix print "@$_\n" for @data;


Replies are listed 'Best First'.
Re^2: How to swap rows with columns?
by FunkyMonk (Chancellor) on Oct 09, 2007 at 22:10 UTC
    For a beginner, do you really think that
    my $rows = () = $inp =~/(?<=^)\w/msg;

    is non-obfuscated?

    I'd rate that as beginner-obfuscated because you use...

    • =()=
    • (?<=^) in your regexp
    • /msg as a regexp modifier
    I think these are a bit "in at the deep end" for someone new to Perl.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2021-09-27 01:26 GMT
Find Nodes?
    Voting Booth?

    No recent polls found