Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: How to swap rows with columns?

by jdporter (Paladin)
on Oct 09, 2007 at 20:47 UTC ( [id://643798]=note: print w/replies, xml ) Need Help??


in reply to Re: How to swap rows with columns?
in thread How to swap rows with columns?

Eh; too much pushing! ;-)

sub transpose { local *_'_; map { $_'_ = $_; [ map $_->[$_'_], @_ ] } 0 .. $#{$_[0]} }

Replies are listed 'Best First'.
Re^3: How to swap rows with columns?
by ikegami (Patriarch) on Oct 09, 2007 at 21:05 UTC

    That doesn't do what my code does at all, which is to avoid the need to fix the array by transposing it by creating it right in the first place. From the point of view, your code is a step backwards. If you wanted to change my code to avoid pushing, then you'd do

    my @data; while (<>) { my @fields = split ' '; my $col = $. - 1; for my $row (0..$#fields) { $data[$row][$col] = $fields[$row]; } }

    Tested.

      That doesn't do what my code does at all.

      Maybe not, but it does what the OP requested, namely, transpose a 2-d array.

Re^3: How to swap rows with columns?
by NetWallah (Canon) on Oct 10, 2007 at 03:44 UTC
    Good code, but with that very high sigil-to-text ratio, I was scrambling Google to find out what "*_'_" meant. (FWIW, I did not find the answer in Google, or the docs, but figured it out experimentally). (Side note - the single-quote also threw off my editor's perl-highlighter).

    Eventually, I decided I would not write code like that, fascinating as it is - it turns out to be just a way to avoid declaring a local variable. It works just as well replacing all instances of that and "$_'_" with "$Column_Index", and becomes a lot more readable ("local *_'_" being replaced by "my $Column_Index" helps by functioning without resorting to "no strict 'refs'").

    I'm hoping this node helps other monks who may not have unraveled "_'_".

    I'm also curious as to how this would be written in idiomatic perl6.

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

        Too much shifting for his taste, surely :)

      I'm hoping this node helps other monks who may not have unraveled "_'_".

      You didn't say what it was. ' is Perl4 for ::. It's still supported in Perl5, by it shouldn't be used in new code. This is documented in the second paragraph of perlmod.

      $_::_ is just some arbitrary variable. Despite it's appearance to $_, it's not a special variable.

      Yeah, I was just having a bit of fun with that.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-16 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found