LexPl has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Filter output based on values
by Corion (Patriarch) on Nov 18, 2024 at 18:26 UTC | |
|
Re: Filter output based on values
by haj (Vicar) on Nov 18, 2024 at 21:17 UTC | |
|
Re: Filter output based on values
by InfiniteSilence (Curate) on Nov 20, 2024 at 07:33 UTC |