Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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;
Regards

mwa

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2021-01-16 18:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?