Re: move down 2 lines in file?
by almut (Canon) on Jul 11, 2008 at 14:55 UTC
|
In case you want to get rid of the $_ = <FILE>, you could also
write something like
my $skip_n = 0;
while (<FILE>) {
next if $skip_n && $skip_n--;
if (/blah/) {
$skip_n = 2; # schedule the skipping of two lines
}
}
| [reply] [d/l] [select] |
|
++
The beauty of this approach is that you don't find your code trying to read past the end of the file just to skip lines.
| [reply] |
|
Also, if you don't know how many lines to skip but you *do* know what the first line-of-interest starts with, you can say something like (untested):
my $good_start = 0;
while (<FILE>) {
next unless (($good_start) || (/^start-of-data pattern/);
$good_start = 1;
# do things with the interesting lines
}
| [reply] [d/l] |
Re: move down 2 lines in file?
by apl (Monsignor) on Jul 11, 2008 at 14:40 UTC
|
Maintain a state flag, and do the extra read once at the end of the loop. For example:
open (file.....)
while (<FILE>) {
my $skip_next = 0;
if /blah/ {
$skip_next = 1;
}
elsif ( ... something else ... ) {
}
elsif ( ... third condition ... ) {
$skip_next = 1;
}
....
$_ = <FILE> if $skip_next;
} # while reading through the file
Revised: If you need to skip a variable number of lines, you could set $skip_next to the number of lines you wish to skip, and use a loop rather than the final assignment.
| [reply] [d/l] |
Re: move down 2 lines in file?
by Narveson (Chaplain) on Jul 11, 2008 at 16:19 UTC
|
my $lines_to_skip = 2 or 3; # or whatever
for (1..$lines_to_skip) { <FILE> }
| [reply] [d/l] |
Re: move down 2 lines in file?
by Lawliet (Curate) on Jul 11, 2008 at 15:56 UTC
|
Hmm, not sure about this method, but it works in theory.
open (file.....)
while (<FILE>) {
$. = 2; # $. holds the number of the last read line
.....
I am not sure if it should be the one I posted above or next if $. == 1 or if $. == 2 though. I have no time to check right now. (Also, I am not sure if it starts counting at 0 or 1. Change the numbers accordingly
<(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
| [reply] [d/l] [select] |
|
Changing $. does not move the file pointer. The only way to skip ahead without actually reading would be to use seek() which doesn't mix well with buffered variable-length line-oriented operations.
-- [ e d @ h a l l e y . c c ]
| [reply] [d/l] [select] |
|
Oh, alright. I thought my idea was pretty cool though~
Can you (or anyone) confirm if the next if... method works?
<(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
| [reply] [d/l] [select] |
|
Re: move down 2 lines in file?
by b17m4p (Initiate) on Jul 12, 2008 at 05:27 UTC
|
I would just do something like:
open FILE, "<", "try.dt" or die $!;
while (<FILE>) {
next if $. < 2;
print $_;
}
And not think twice about it; is there something wrong with that?
| [reply] |
|
open FILE, "<", "try.dt" or die $!;
while (<FILE>) {
next if $. <= 2;
print $_;
}
| [reply] [d/l] |
Re: move down 2 lines in file?
by mvaradhan (Initiate) on Jul 13, 2008 at 14:56 UTC
|
open (FILE, "<", "test") or die "can't open" ;
sub SKPLN($) {($_[0]!=0) and <FILE> and SKPLN($_[0]-1)}
while (<FILE>) {
SKPLN(1), next if /blah/ ;
SKPLN(2), next if second condition ;
.....
}
| [reply] [d/l] |
Re: move down 2 lines in file?
by esteban (Initiate) on Jul 13, 2008 at 07:03 UTC
|
Hello, what you do seems right, but could do with some improvement. Consider not asigning the line you want to skip to anything. And if you want to skip a arbitrary number of lines you can use a foreach loop.
open (file.....)
while (<FILE>) {
if /blah/ {
<FILE>; # Skip one line
<FILE> foreach(1..$skip); # Skip a numer of lines
| [reply] [d/l] |