empty array?
am 11.04.2008 17:48:42 von ela
Could anybody tell me why @x in the last line contains nothing? Thanks a
lot.
$filename = $ARGV[0];
open(FP, $filename);
@x=();
@y=();
$i=0;
while ($line = )
{
chomp $line;
print $line;
@words = split(/\t/, $line);
$x[$i] = $words[1];
$y[$i++] = $words[2];
}
print @x;
Re: empty array?
am 11.04.2008 18:02:38 von RedGrittyBrick
Ela wrote:
> Could anybody tell me why @x in the last line contains nothing? Thanks a
> lot.
Have you tried starting your program with
use strict;
use warnings;
>
> $filename = $ARGV[0];
>
> open(FP, $filename);
Perhaps this failed?
Most people would check
open my $FP, '<', $filename
or die "can't open '$filename' - $!";
> @x=();
> @y=();
my @x;
my @y;
> $i=0;
> while ($line = )
> {
> chomp $line;
> print $line;
> @words = split(/\t/, $line);
Each word in your data is separated by a single tab?
There are no leading tabs?
print "DEBUG: [", join ("], [", @words, "]\n";
>
> $x[$i] = $words[1];
@words is a zero-based array. Presumably you intended to pick out only
the second item of each line?
Perhaps there isn't a second item in your data?
> $y[$i++] = $words[2];
> }
> print @x;
>
--
RGB
Re: empty array?
am 11.04.2008 20:19:36 von someone
Ela wrote:
> Could anybody tell me why @x in the last line contains nothing?
How do you know it contains nothing?
> Thanks a
> lot.
>
> $filename = $ARGV[0];
my $filename = $ARGV[0];
> open(FP, $filename);
open FP, '<', $filename or die "Cannot open '$filename' $!";
> @x=();
> @y=();
> $i=0;
my @x;
my @y;
> while ($line = )
while ( my $line = )
> {
> chomp $line;
> print $line;
> @words = split(/\t/, $line);
my @words = split(/\t/, $line);
> $x[$i] = $words[1];
> $y[$i++] = $words[2];
perldoc -f push
push @x, $words[1];
push @y, $words[2];
> }
> print @x;
print "The length of \@x is ", scalar @x, "\n";
print "The contents of \@x are", map( " '$_'", @x ), "\n";
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Re: empty array?
am 11.04.2008 22:26:07 von 1usa
"Ela" wrote in
news:fto18u$cht$1@ijustice.itsc.cuhk.edu.hk:
> Could anybody tell me why @x in the last line contains nothing?
You could figure that out your self, you know.
> $filename = $ARGV[0];
use strict;
use warnings;
my ($filename) = @ARGV;
> open(FP, $filename);
open my $FP, '<', $filename
or die "Cannot open '$filename': $!";
> @x=();
> @y=();
> $i=0;
my (@x, @y);
> while ($line = ) {
while ( my $line = ) {
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 = ) {
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::Dumper;
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, line 3.
$VAR1 = [
'2',
'b'
];
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/