in reply to Packet Patterns redux - my script can't count
This is potentially inaccurate because if the ASCII representation of $str includes any regular expression metacharacters (like dots) it may count things that should not match. It is also potentially dangerous because some of those characters may cause your program to crash because they form an invalid regex. You could get around that by using \Q and \E, like this:my @temparr = ($packets[$pnum] =~ /$str/g);
The second thing is that the logic of your loops seems a little too convoluted. It could probably be rewritten as something like this (untested):my @temparr = ($packets[$pnum] =~ /\Q$str\E/g);
The map builds a list with the count of how many times $str appears in each element from @packets, and the for in that same line adds all those counts to the corresponding element of %all. I think it's essentially the same algorithm you had before, except that it does count the current packet in the matches.foreach my $p (@packets) { foreach my $l (2..length($p)) { foreach my $pos (0..length($p)-$l) { my $str=substr($p, $pos, $l); $all{$str}+=$_ for map { scalar(($_ =~ /\Q$str\E/g)) } @packets; } } }
--ZZamboni
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(Guildenstern) RE: Re: Packet Patterns redux - my script can't count
by Guildenstern (Deacon) on Aug 08, 2000 at 22:28 UTC |