develooper Front page | perl.beginners | Postings from January 2012

rows to columns issue

Thread Next
From:
Rajeev Prasad
Date:
January 9, 2012 13:08
Subject:
rows to columns issue
Message ID:
1326143292.78570.YahooMailNeo@web114205.mail.gq1.yahoo.com


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 :(



#!/usr/bin/perl

use strict;
use warnings;

my ($IN_FH, $line,$string);

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>) {
chomp($line);
my @arr_."$counter"=split(/ /,$line);
$counter++;
}
close $IN_FH;

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


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About