empty array?

E

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 = <FP>)
{
chomp $line;
print $line;
@words = split(/\t/, $line);

$x[$i] = $words[1];
$y[$i++] = $words[2];
}
print @x;
 
R

RedGrittyBrick

Ela said:
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;
 
J

John W. Krahn

Ela said:
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 = said:
{
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
 
A

A. Sinan Unur

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 <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top