Reputation: 2214
I am working with two files, so as I loop through one I need to look through part of another one, and the information I encounter in it will be sequential. So I thought the best way would be to keep track of the line number:
open(PARSED, "< file.txt") or die$!;
my @parsedFile = < PARSED >;
my $line = 0;
my $size = @parsedFile;
# This next part is in a loop, but the only important thing is this next line
print $parsedFile[$line];
Even as the value of $line increases, it prints nothing but if I do the following there is no problem:
I have even tried a number of variations of trying to pull individual lines from @parsedFile but with no luck.
foreach (@parsedFile){
print $_;
}
Upvotes: 0
Views: 543
Reputation: 1259
The original problem was fixed by ysth, but if the files aren't huge (you are reading them into memory, so I'm guessing not), why not use Tie::File instead of all those shenanigans?
use strict;
use warnings;
use 5.010;
use Tie::File;
tie my @parsedFile, 'Tie::File', 'file.txt' or die "Error: $!";
my $line = 0;
print $parsedFile[$line];
Upvotes: 1
Reputation: 98388
<>
is an incredibly picky operator that does two very different things based solely on precise syntax of what's in the brackets. < PARSED >
(with the extra spaces) is glob("PARSED"), not readline(PARSED), so your array is just getting the single string "PARSED".
(Assuming your posted code is accurate; it really helps if you copy and paste your actual not-working code, not re-type parts of it; it helps even more if your copy-and-pasted code can be run exactly as is to demonstrate your problem.)
Note that:
use warnings;
open(PARSED, "< filename") or die $!;
my @lines = < PARSED >;
will give you a warning that PARSED is used only once, a big clue that the <>
isn't doing what you think.
Upvotes: 7
Reputation: 455000
To load the file lines in an array you need to open the file first:
open F,'<','file.txt' or die;
my @parsedFile = <F>;
The way you are doing it results in array parsedFile
which contains only one element "file.txt"
which is the name of the file.
Upvotes: 0