in reply to Counting between the lines

A hash might be one way.
#!/usr/bin/perl use warnings; use strict; my %hash; my $name; while (<DATA>){ if (/word\s(\d+)/){ $name = $1; } elsif (! /#/){ $hash{$name}++; } } for my $key (keys %hash){ print "$key $hash{$key}\n"; } __DATA__ # run1 # word 26871 # text returns text text text # run2 # word 26872 # text returns text text #run3
Output:
26871 3 26872 2

Replies are listed 'Best First'.
Re^2: Counting between the lines
by gaal (Parson) on Dec 14, 2004 at 07:35 UTC
    As long as the "current word" doesn't return to being one for which we'd already starting counting lines, there's no need to keep the data in memory. Emit it as you gather enough of it.