in reply to Re: How to swap rows with columns? - Still unresolved :(
in thread How to swap rows with columns? - Still unresolved :(

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

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

    You attempt to read from $data, yet never open it that file handle.
    You changed the initial content of @data. (It should be empty.)
    DATA shouldn't be used as an all-purpose file handle. It's a special file handle.

    Fix:

    #!/usr/bin/perl -w use strict; my $data_qn = 'data_file'; open($data_fh, '<', $data_qn) or die("Unable to open plot data \"$data_qn\": $!\n"); my @data; while (<$data_fh>) { my @fields = split ' '; for my $row (0..$#fields) { push @{$data[$row]}, $fields[$row]; } } use Data::Dumper; print Dumper \@data;

    Update: Oops, forgot to re-add the my @data; that was removed. Fixed.

Re^3: How to swap rows with columns? - Still unresolved :(
by mr_mischief (Monsignor) on Oct 11, 2007 at 16:08 UTC
    #!/usr/local/bin/perl -w use strict; my @data; my $file='data_file'; open( DATA , $file); @data=<DATA>; ### already read all of $file into @data close(DATA); print @data; while (<$data>) { ### can make this 'for ( @data ) {' my @fields = split ' '; for my $row (0..$#fields) { push @{$data[$row]}, $fields[$row]; } } use Data::Dumper; print Dumper \@data;
    Another solution would be to ditch the
    @data=<DATA>;
    and change the while loop header to:
    while ( <DATA> ) {
    The @data array in your version is taking the lines of the file in list context, so it takes all of them. The for loop can then just use the array. If you want to read just one line at a time into memory, you can use the while ( <DATA> ) { version.

    While you're at it, you might want to look into lexical filehandles instead of barewords. Lexical filehandles really are the way to go.

    Update: The for loop suggestion won't work because you'd be using the same array as what you're pushing into. I didn't notice they had the same name until I came back through the thread and saw ikegami's node. In order to use that, you'd have to change the name of one or the other.

Re^3: How to swap rows with columns? - Still unresolved :(
by blazar (Canon) on Oct 12, 2007 at 12:25 UTC
    The script works fine if I use "./<script_name> data_file". But when I hardcode the name of data_file, it doesn't work. I am sure it has something to do with "while (<>)" statement. Can anyone please advice how to proceed when we have datafile name hardcoded, and it is read in an array and then.......???

    I personally believe that you should get familiar with some basic Perl: then you wouldn't be asking such an embarassingly trivial question. Anyway, here's your fish - you either want

    • an explicit open as in:
      open my $fh, '<', $data_file or die "Can't open `$data_file': $!\n";
      and then you would use while (<$fh>) instead of while (<>), or
    • just populate @ARGV manually:
      @ARGV='data_file';