Well, I certainly am glad I just picked up the new Camel book. I had been having quite a time trying to find patterns common to multiple strings. I had sample input of two strings "ABCD". For each 1,2,3 and 4 character substring there should have been 2 matches, right? Not so. My original code looked liked this (relevant part only):
$all{$str} += $packets[$_] =~ /$str/g;
When I ran it against the sample input, I got weird output. For example, two character substring matches:
- AB - 2 matches (good!)
- BC - 0 matches (eh?)
- CD - 2 matches (good again!)
Any combination would produce the weird 2,0,2,0, etc pattern of matching.
Following much frenzied thumbing through the Camel, I ran across almost an offhand mention to pos. I added the line
pos $_ = 0; after the previous line, and now the counts work perfectly.
I feel good that I managed to find this one by reading the FM, but also feel that I should mention it since it never came up in any of the (wonderful!) responses I've received to the
pattern matching problem and
its revisit.
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.