Okay. As some of you may remember, I asked a question a couple of days ago about comparing packet data to find common substrings. (See
My Original Post)
I have written some code (see below) that appears to produce valid output. However, when I was looking through the output, I've noticed that some of the substring match counts are wrong. For a sample run, I copied the same packet data four times, which should have made the output a little easier to check.
The first line of output is a substring that contains all of the packet data, so it makes sense that there are four matches. The second line bothers me, though. It's a substring length($packet)-1 in size. Eye-grepping the input, I can see that this should have four matches as well, but when I run the script I get 7!
I'm not asking for an in-depth analysis, but I've been looking at it so much in the last couple of days that I may be missing something obvious. Any help is appreciated!
die "No file name" unless @ARGV;
my @packets = ();
my $short = 9999;
while (<>)
{
push(@packets,$_);
if (length($_)-1 < $short)
{
$short = length($_) -1; # Find shortest packet
}
}
chop(@packets);
my $maxlen =0;
my $index = 0;
my $curpack;
my %all;
foreach $curpack (@packets)
{
#Iterate over packets
$maxlen = length($curpack);
for (my $fsize = 2; $fsize <= $maxlen; $fsize ++)
{
#Check size 2..$maxlen substrings
for (my $pnum = 0; $pnum < @packets; $pnum ++)
{
#Iterate over packets to check against
for (0..($maxlen-$fsize+1))
{
#Don't compare against self or shorter strings
if (($index != $pnum) && ($fsize <= length($packets[$pnum])))
{
my $str = substr($curpack,$_,$fsize);
my @temparr = ($packets[$pnum] =~ /$str/g);
my $nmatch = @temparr;
if (defined($all{$str}))
{
$all{$str} += $nmatch;
}
else
{
#Remember to count self
$all{$str} = $nmatch + 1;
}
undef @temparr;
}
}
}
}
$index ++;
print "Comparing packet ",$index," to all others:\n";
my $value;
my @sort = sort {length($b) <=> length($a)} keys %all;
foreach $value (@sort)
{
#Show only results in >1 packet
if ($all{$value} > 1)
{
print "(",length($value),") $value: $all{$value} times\n";
}
delete($all{$value});
}
undef @sort;
}
exit;
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.