|
Prev: FAQ 9.6 How do I download a file from the user's machine? How do I open a file on another machine?
Next: Gisle Aas's Illustrated Perl Guts?
From: Ela on 11 Apr 2008 11:48 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 = <FP>) { chomp $line; print $line; @words = split(/\t/, $line); $x[$i] = $words[1]; $y[$i++] = $words[2]; } print @x;
From: RedGrittyBrick on 11 Apr 2008 12:02 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 = <FP>) > { > 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
From: John W. Krahn on 11 Apr 2008 14:19 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 = <FP>) while ( my $line = <FP> ) > { > 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
From: A. Sinan Unur on 11 Apr 2008 16:26
"Ela" <ela(a)yantai.org> wrote in news:fto18u$cht$1(a)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 = <FP>) { 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::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, <DATA> line 3. $VAR1 = [ '2', 'b' ]; -- A. Sinan Unur <1usa(a)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/ |