"Max" <> wrote in message
news:<mJlAc.2227$ m>...
> If someone has a few minutes, can you tell me what I'm doing wrong.
From what I can tell, you've made one or two logic errors (you're not telling
the program to do what you want), and then a few stylistic "errors". The
problem, I must assume, is that you use 'lt' and 'gt', rather than '<'
and '>'. The former are for string comparison, the latter for numeric
comparison. That is:
'10' lt '5'
10 > 5
I assume you wanted the numeric comparison. Also, if you input 5 and 8 for
$start and $stop, just lines 6, 7, and 8 are copied. You may want to futz
with the boundaries.
I also made some stylistic changes to the code. The most important is adding
'use strict;' and 'use warnings;', which are the most helpful things in Perl
since spliced bread. The others are less critical, but in the absence of any
preformed habits otherwise, there's no reason not to just use the
three-argument form of open all the time (for example).
One other thought -- if you don't want to keep track of it youself, the
variable $. (dollar-period) contains the current line number of the last
filehandle that you've read from. As long as you're not messing with $/
(which redefines for perl what a line is), $. should always correspond
to your $cnt.
Hope this helps. Code is tested but (of course) not guaranteed.
#!/usr/bin/perl
use strict;
use warnings;
my ($file, $start, $stop) = @ARGV;
print "$start\n$stop\n";
my $cnt = 0;
open (IN, '<', $file) or die "Unable to open $file: $!";
open (OUT, '>', $file . '-split') or die "Unable to open $file-split: $!";
while (<IN>) {
chomp;
$cnt++;
next if ($cnt < $start);
last if ($cnt > $stop);
print OUT "$_\n";
}
close (IN);
close (OUT);
|