Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: How to swap rows with columns?

by snopal (Pilgrim)
on Oct 09, 2007 at 21:09 UTC ( #643802=note: print w/replies, xml ) Need Help??


in reply to How to swap rows with columns?

Really! How hard is it to transpose the indexes of a pair of foreach loops?

Replies are listed 'Best First'.
Re^2: How to swap rows with columns?
by snopal (Pilgrim) on Oct 09, 2007 at 21:38 UTC

    Isn't this so much easier to understand?

    #!/usr/bin/perl use warnings; use strict; my @in = ([1,2,3,4,'a','b','c'], ['z','y','x','w',9,8,7], ['e','f','g','h',5,4,6], ); my @out; for my $y (0..(@in-1)) { for my $x (0..(@{$in[$y]}-1)) { $out[$x][$y] = $in[$y][$x]; } }

    No need to be a Perl geek to do this right! And think of the ease of maintenance!

      for my $y (0..(@in-1)) { for my $x (0..(@{$in[$y]}-1)) {

      I'm puzzled as to why you do the subtractions to find the upper bound of the arrays rather than use the $#array that Perl provides.

      for my $y ( 0 .. $#in ) { for my $x ( 0 .. $#{$in[$y]} ) {

      Cheers,

      JohnGG

        My original did, but the second loop definition was looking like line noise and I was demonstrating the practice to a novice. So I opted for the more verbose, and less obtuse example.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2020-10-28 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (264 votes). Check out past polls.

    Notices?