in reply to Filter output based on values

The check $tally[i] ne '0' assumes that you initialized the elements of @tally. Probably the untouched elements of @tally are still undef.

You can simplify your check:

if ($tally[$i]) { printf {$out} "%-20s %30d \n", $regex, $tally[$i] // 0 ; }

To align the occurrences you can find out the longest regex in advance and use this length in your format. The formats for printf are strings, so Perl will happily interpolate a variable into them!

Here's a complete example. I use a bit of map and grep magic to get the longest regex which actually occurs in the text.

use 5.032; use autodie; use List::Util qw( max ); use File::Temp qw( tempfile ); my $xml = <<END; Electric monks believed things for you, thus saving you what was becoming an increasingly onerous task, that of believing all the things the world expected you to believe... The new improved Monk Plus models were twice as powerful, had an entirely new multi-tasking Negative Capability feature that allowed them to hold up to 16 entirely different and contradictory ideas in memory simultaneously without generating any irritating system errors. -- Douglas Adams, Dirk Gently's Holistic Detective Agency END my @tally; my @regexes = ( qr/\bmonk/, qr/\bmonk\b/, qr/\bmonk\b/i, qr/Douglas Adams, Dirk Gently's Holistic Detective Agency/, qr/Douglas Adams, The Hitchhikers Guide To The Galaxy (A trilogy i +n five)/ ); for my $i (0 .. $#regexes) { my $regex = $regexes[$i]; ++$tally[$i] while $xml =~ /$regex/g; } my $max_length = max(map { length "$_" } @regexes[ grep { $tally[$_] } (0 .. $#regexes) ]) +; my ($out,$path) = tempfile( CLEANUP => 0); say "Your output is available at '$path'"; for my $i (0 .. $#regexes) { my $regex = $regexes[$i]; $regex =~ s/^\(\?\^://; $regex =~ s/\)$//; if ($tally[$i]) { printf {$out} "%-${max_length}s %3d\n", $regex, $tally[$i] // +0 ; } }