Could anybody tell me why @x in the last line contains nothing?
You could figure that out your self, you know.
use strict;
use warnings;
my ($filename) = @ARGV;
open my $FP, '<', $filename
or die "Cannot open '$filename': $!";
my (@x, @y);
while ( my $line = <FP> ) {
last if $line =~ /^\s+$/; # end the loop
# if there is nothing
# but whitespace
chomp $line;
@words = split(/\t/, $line);
I don't know if you are expecting more than two tab separated
fields.
my @words = split /\t/, $line;
$x[$i] = $words[1];
$y[$i++] = $words[2];
Is $words[1] an error or did you really mean to refer to the second
element of @words?
push @x, $words[1];
push @y, $words[2];
Anyway, here is a revised version of the program that does away with
$i. Also, I replaced @words with two scalar variables: No point in
putting the other fields in @words if you are not going to use them.
#!/usr/bin/perl
use strict;
use warnings;
my (@x, @y);
while ( my $line = <DATA> ) {
last if $line =~ /^\s+$/;
unless ( $line =~ /^[^\t](?:\t[^\t]+)+$/ ) {
warn "Fields are not separated by tabs:\n'$line'";
next;
}
my (undef, $wx, $wy) = split /\t/, $line;
push @x, $wx;
push @y, $wy;
}
use Data:
umper;
print Dumper \@x;
__DATA__
1 2 3 4 5 6
a b c d e f
alpha beta gamma delta epsilon zeta
C:\DOCUME~1\asu1\LOCALS~1\Temp> t1
Fields are not separated by tabs:
'alpha beta gamma delta epsilon zeta
' at C:\DOCUME~1\asu1\LOCALS~1\Temp\t1.pl line 12, <DATA> line 3.
$VAR1 = [
'2',
'b'
];
--
A. Sinan Unur <
[email protected]>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/