in reply to Filter output based on values
Not a great improvement on the other solutions (below). However, making a single structure saves space and allows for a way to easily see things in the debugger (as shown). Also, no need to scan the list again to obtain the max length.
#!/usr/bin/perl -w use strict; my $maxLen = 0; my $aline = qq~an interesting line of text I would like to scan I see +I see I see~; my @regexes = (['first',qr/should fail/,0] , ['second','like',0] , ['t +hird',qr/I/,0] , ['fourth',qr/scan/,0]); sub niceOutput {for(@regexes){ printf "%-${main::maxLen}s %30d\n", $_- +>[1],$_->[2] if $_->[2] > 0} } sub resetRegexes {for(@regexes){$_->[2] = 0}} sub rexyScan {my $Len = 0; for my $n(@regexes){ ++$n->[2] while ($ali +ne=~m/$n->[1]/g); $Len = length $n->[1]; if($Len > $maxLen) { $main:: +maxLen = $Len } }; } &rexyScan; &niceOutput;
Debugger shows @regexes:
main::(-e:1): 0 DB<1> @regexes = (['first',qr/interesting/,0] , ['second','like',0] +, ['third',qr/I/,0] , ['fourth',qr/scan/,0]); DB<2> x @regexes 0 ARRAY(0x569bf962c388) 0 'first' 1 (?^u:interesting) -> qr/(?^u:interesting)/ 2 0 1 ARRAY(0x569bf95f6650) 0 'second' 1 'like' 2 0 2 ARRAY(0x569bf95f68d8) 0 'third' 1 (?^u:I) -> qr/(?^u:I)/ 2 0 3 ARRAY(0x569bf95f6428) 0 'fourth' 1 (?^u:scan) -> qr/(?^u:scan)/ 2 0 DB<3>
Celebrate Intellectual Diversity
|
|---|