in reply to RE: Shot myself in the foot with a pos
in thread Shot myself in the foot with a pos

did you mean
if ($packets[$i] =~ /$str/) {?
The only thing that I see with this is if there's multiple instances of $str in $packets$i, the hash counter won't be incremented correctly. If I leave the /g in, my matches won't happen because pos is past the start of a match.
For example, I have ABC and I check (and match the substring AB). This means that pos points at C now. If I go through again and try to match the substring BC, it won't find it.
Maybe I've misunderstood what you were suggesting here. Correct me if necessary.

As far as the solution you provided, I haven't tried it yet. To be completely honest, I'm just trying to get myself into the groove of learning the minor nuances of Perl, and I have very little idea of what parts of your code are doing. Fear not, though - I am going through it bit by bit with my Camel book at hand, and hopefully in the next day or two I'll feel confident enough to run it. I'd like to at least be able to tweak things if I need to and know what I'm doing! :)

Replies are listed 'Best First'.
RE (3): Shot myself in the foot with a pos
by tilly (Archbishop) on Aug 11, 2000 at 00:55 UTC
    Perl only cares about pos() for matches with the /g modifier. Drop the /g, and all attempts at matching start from the beginning of the string.

    As for the code I provided, well good luck deciphering it. The algorithm I used is pretty complex. The key idea though is for each packet to maintain a hash whose keys are the substrings we are still interested in, and whose values are the set of positions in that packet where the pattern starts. :-)

    I don't tend to write production code like that....