Thank you Jim,

I changed my approach to array of array. I am getting right result but 
something seem to be messed up....


$ cat testfile123
a b c d e f
1 2 3 4 5 6


$ row2col.pl testfile123
Use of uninitialized value in concatenation (.) or string at ./row2col.pl line 
29.
a 1  
Use of uninitialized value in concatenation (.) or string at ./row2col.pl line 
29.
b 2  
Use of uninitialized value in concatenation (.) or string at ./row2col.pl line 
29.
c 3  
Use of uninitialized value in concatenation (.) or string at ./row2col.pl line 
29.
d 4  
Use of uninitialized value in concatenation (.) or string at ./row2col.pl line 
29.
e 5  
Use of uninitialized value in concatenation (.) or string at ./row2col.pl line 
29.
f 6  
Use of uninitialized value in concatenation (.) or string at ./row2col.pl line 
29.
Use of uninitialized value in concatenation (.) or string at ./row2col.pl line 
29.
Use of uninitialized value in concatenation (.) or string at ./row2col.pl line 
29.
 


CODE:
#!/usr/bin/perl

use strict;
use warnings;

my $rowfile= $ARGV[0];
my @aoa;

open (my $IN_FH,"<",$rowfile) or die "could not open $rowfile: $!";

my $row=0;
my $col=0;
while (my $line = <$IN_FH>) {
    chomp($line);
    my @tmp=split(/ /,$line);
    for (my $y=0;$y<=@tmp;$y++) {
        $aoa[$row][$y] = $tmp[$y];
    }
    $row++;
    if ( @tmp > $col) { $col = @tmp;}
}
close $IN_FH;

for (my $x=0;$x<=$col;$x++) {
    for (my $y=0;$y<=$row;$y++) {
        print $aoa[$y][$x]." ";
    }
    print "\n";
}




----- Original Message -----
From: Jim Gibson <jimsgib...@gmail.com>
To: perl list <beginners@perl.org>
Cc: 
Sent: Monday, January 9, 2012 3:29 PM
Subject: Re: rows to columns issue

On 1/9/12 Mon  Jan 9, 2012  1:08 PM, "Rajeev Prasad" <rp.ne...@yahoo.com>
scribbled:

> 
> 
> Hello,
> 
> I tried following code but it is not working.
> 
> I have a file with many lines(records) where field separator is space.
> 
> I want to convert rows to columns, something like...
> 
> source:
> a b c d
> 1 2 3 4
> 
> output:
> a 1
> b 2
> c 3
> d 4
> 
> 
> 
> Here is my test code: It is not working :(

It would be helpful if you could tell us exactly how it is not working. Does
the program not compile? Does is not produce an output file? Is the output
file not what you expect? Do you get an error message when you run it?

> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> my ($IN_FH, $line,$string);

It is better to declare these three variables when they are first used.

> 
> my $rowfile= $ARGV[0];
> my $colfile= $rowfile.".row2col";
> 
> open ($IN_FH,"<",$rowfile) or die "could not open $rowfile: $!";
> #open ($OUT_FH,">",$colfile) or die "could not open $colfile: $!";
> 
> my $counter=1;
> while ($line = <IN_FH>) {

That should be <$IN_FH>. IN_FH and $IN_FH are two different things.

> chomp($line);
> my @arr_."$counter"=split(/ /,$line);

I do not know what the above line does or is supposed to do. Are you trying
to use dynamically-generated variable names? That is rarely a good idea. Use
an array-of-arrays instead.


> $counter++;
> }
> close $IN_FH;
> 
> for (my $i=1;$i<=150;$i++) {
>     for (my $x=1;$x<=$counter;$x++) {
>         $string .= $arr_."$x".[$i]." ";
>     }
>     print $string;
> }



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to