I have got a large number of specific regexes - stored in an array @regexes. My aim is to get a statistics which tells me what regexes occur in the input file and how often each one occurs.

A count loop accumulates the number of occurrences for each $regex from the @regexes in a variable $tally.

for my $i (0 .. $#regexes) { my $regex = $regexes[$i]; ++$tally[$i] while $xml =~ /$regex/g; }

Later I have an output loop that prints each $regex and its number of occurrences ($tally[i]).

for my $i (0 .. $#regexes) { my $regex = $regexes[$i]; $regex =~ s/^\(\?\^://; $regex =~ s/\)$//; printf {$out} "%-20s %30d \n", $regex, $tally[$i] // 0 ; }

Now I would like to exclude any regex from the output, which doesn't occur in my file ($tally[i] ne '0'). But my idea to wrap the printf statement in an if-statement doesn't work.

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

Please bear with me when I add a little secondary problem to this ticket. How could I describe the output format in printf so that all occurrences will be aligned properly independent from the length of the regex string?


In reply to 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.