Aditi Gupta <[EMAIL PROTECTED]> wrote:
>  to extract data columnwise from a file, the following code 
> was written with the help of perl experts of this list. the 
> script is as follows:

> #!usr/bin/perl
> use warnings;

use strict;

> my %hash;
> 
> $file= "try.txt";
> 
> open (FH, $file) or die;
> @rows=<FH>;

1) my @rows;

2) slurping a file is bad. Instead, use

while( my $line = <FH> ){

> chomp $line;
> $x = length $line;
> 
> for (my $j=0; $j<$x; $x++)

This loop will never terminate - you initialize $j, but increment $x.

> # to get value in each column of $line
> 
> push @{$hash{$j}}, substr $line,$j,1;

Instead, I'd recommend

my $j = 0;

foreach my $char ( split //, $line ){
  push @{$hash{$j++}}, $char;
}

> }
> }
> 
> foreach my $hkey (keys %hash) {
> print "@{$hash{$hkey}}\n";

Wouldn't make sorting things look better?

foreach my $hkey (sort {$a <=> $b} keys %hash) {
  print "@{$hash{$hkey}}\n";
}

HTH,
Thomas

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to