Jm lists wrote:
2007/3/16, Rob Dixon <[EMAIL PROTECTED]>:
Jm lists wrote:
hello lists,
please see the codes below:
[snip code, repeated below]
the itemid.txt has 470 lines data,looks like:
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
But when I run that script I got the output:
$ perl test.pl
1693
why? my purpose is to create an arrany which include all the text lines.
It is very hard to believe that both your program and your data file are
as you say. If I run exactly the same program against the same data I get
the output:
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
which is what I would expect.
Please make sure you are opening the correct data file and that its
contents
are what you think they are.
These are copied from the screen:
$ cat test.pl
use strict;
use warnings;
my @arr = ();
open HD,"itemid.txt" or die $!;
while(<HD>){
chomp;
push @arr,$_;
}
close HD;
print "@arr";
$ tail -20 itemid.txt
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
$ perl test.pl
1693
It's really happened to me. So faint!
<clatters to the floor>
OK, but you didn't tell us that it was the /last/ number in your list that was
being printed! The output you showed didn't appear in the list of data at all,
which is why I thought you were reading different data from what you expected.
Tom and John are probably right - you have lines terminated by <CR><LF>, so the
call to chomp leaves just the carriage return. Each line then overwrites the
previous one on output without ever advancing to the next line. Check this by
changing your last print line to
print scalar @arr, " lines found:\n";
print "$_\n" foreach @arr;
when the data will magically appear.
You will also need to remove the additional control character, so removing all
trailing space as John suggested by replacing chomp with s/\s+\z// will put you
back on course.
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/