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.