MLUG: RE: [MLUG] grepping the 9th line after a matching line
RE: [MLUG] grepping the 9th line after a matching line
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Improvement (memory efficiency): 

#!/usr/bin/perl
use strict;
use warnings;

if ( not defined $ARGV[0] ) {
	die "Usage:  $0 <file>\n\n";
}

# I'll leave out all the other error checking you should be 
# doing here.

my $index = 0;
my @CONTENTS;
open INFILE, "<$ARGV[0]";

PARSE: while ( <INFILE> ) {

	push @CONTENTS, $_;	# Fixed useless index.

	if ( $index < 9 ) {
		next PARSE;  # Don't have enough data yet.
	}

	# We now have a ten-line buffer.
	if ( $CONTENTS[0] =~ /pattern/ ) {
		print $CONTENTS[9];
	}

	# Clear out the first line.  Already checked it.
	# This makes it so ONLY ten lines are in memory
	# at any given time.  You might be able to use
	# undef here instead of a dummy scalar.
	my $devnull = shift @CONTENTS;

	$index++;	# Just in case you still want the
			# actual line number (don't forget
			# to add 1).

}
close INFILE;	# Don't forget!
exit(0);		# Don't forget this either!

_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members