6/30 Perl Q: How do I "rewind" when iterating through a file? I want to
do something like
while (<FILE>) {
if ($_ =~ /pattern1/) {
search backwards/upwards for pattern2
}
}
What's the best way to do this? I've tried decrementing $. but that
doesn't seem to work quite right.
\_ This is probably a "there's a better way to do this, grasshopper"
thing. Search for pattern2 first, then throw it away if pattern1
doesn't exist. Perl likes to move forward through files. The
suggestion below also applies.
\_ If the file is small, you can do something like...
@file = <FILE>;
foreach (@file) {
if (/pattern1/) {
@file2 = @file;
foreach $inner (@file2) { ... /pattern2/ ... }
}
}
Or do a last and restart a search for pattern2. i don't know
what exactly you are trying to do. alternatively, you can open
up a second file handle on the same file... |