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 ; } }

In reply to Re: Filter output based on values by haj
in thread Filter output based on values by LexPl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.