my @codes = qw/ CODE1 CODE2 CODE3 /; my %hitCounts; my @regex = map { $hitCounts{$_}=1; qr/$_/i } @codes; # tune this parameter for optimal performance, balancing better ordering # of regexen with sort costs... my $resortFreq=1000; my $iterCount=0; while (my $inputline = ) { my $found = 0; foreach my $regex(@regex) { if ($inputline =~ /$regex/) { $hitCounts{$regex}++; $found = 1; last; } } # re-sort every 1000 lines. The "1000" is a parameter that prolly should # be tuned if(++$iterCount%$resortFreq == 0) { @regex=sort {$hitCounts{$b}<=>$hitCounts{$a}} @regex; } next unless $found; # logic goes here } #### END { print "Regexen in sorted order:\n\t"; print join "\n\t",sort {$hitCounts{$b}<=>$hitCounts{$a}} @regex; print "\n"; }