perl_geek has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: How to swap rows with columns? - Still unresolved :(
by ikegami (Patriarch) on Oct 11, 2007 at 14:32 UTC

    That's exactly what my code from the other thread does. What's the problem?

    my @data; while (<>) { my @fields = split ' '; for my $row (0..$#fields) { push @{$data[$row]}, $fields[$row]; } } use Data::Dumper; print Dumper \@data;
    17:12:41 0 0 0 100 17:12:42 0 1 0 99 17:12:43 0 0 0 100
    >perl script.pl data $VAR1 = [ [ '17:12:41', '17:12:42', '17:12:43' ], [ '0', '0', '0' ], [ '0', '1', '0' ], [ '0', '0', '0' ], [ '100', '99', '100' ] ];

    PS — Please don't post links to a specific PerlMonks domain. There are more than one and most people are usually only logged in to one of them. Following that link would log them out. Using [id://643792] (How to swap rows with columns?) would be best.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to swap rows with columns? - Still unresolved :(
by Fletch (Bishop) on Oct 11, 2007 at 14:36 UTC

    You got several answers how to transpose an AoA in How to swap rows with columns? already. Why can't you just use one of those, or simply load your input data into your destination array in the order you want it?

    my @data; while( <$in> ) { chomp; my( @cur ) = split( /\s+/, $_ ); push @{ $data[ $_ ] }, $cur[ $_ ] for 0..$#cur; }

    Update: And on closer inspection this is exactly like what you've already been given. I think I'm just going to take the rest of the day off since ikegami's already beaten me to the punch at least twice today . . . %)

Re: How to swap rows with columns? - Still unresolved :(
by heth (Sexton) on Oct 14, 2007 at 12:02 UTC
    Hi :-) When dealing with Perl only a few times a year, i like to keep things so i understand them next time. Here is a simple approach to your problem.
    #!/usr/bin/perl -w use strict; my @sarFormat; my $colCou = 0; open SARDATA, 'C:\temp\rawSarData' or die "HaHa $!\n"; # Check lines for syntax dd:dd:dd dd dd dd dd while ( defined( my $line = <SARDATA>)) { $line =~ m/(\d+:\d+:\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/; if ( defined ( $1 )) { $sarFormat[$colCou][0] = $1; $sarFormat[$colCou][1] = $2; $sarFormat[$colCou][2] = $3; $sarFormat[$colCou][3] = $4; $sarFormat[$colCou][4] = $5; $colCou++; } } for my $newRow (0..4) { for my $newCol ( 0..$#sarFormat ) { print "$sarFormat[$newCol][$newRow] "; } print "\n"; }
    To answer a question - you first of all need to understand, what the person who asked the question understand.