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/


Reply via email to