I'm writing a small Perl script that will take a large list of IP addresses ( > 100), break them down into smaller chunks, and format them for our intrustion detection system (Snort). Basicly it's a var builder (for those Snort afficianados). Problem is that it's not working right, and I or anybody else that I ask around the office knows what's wrong with it. My suspicion is that it's a "string vs integer" thing.

Here's the code bit that parses thru the array of IPs (@fred) and outputs them:

$c = 0; $g = 0; foreach $w (@fred) { if ($g == 0) { print "\$g = $g and \$c = $c\n"; @fred1 = ($varname,$c); $temp_var1 = join("", @fred1); print "var $temp_var1 ["; } if ($g == 20) { print "$w]\n"; print "\$c = $c and \$g = $g\n"; $c++; @fred1 = ($varname,$c); $g = 0; print "\$g = $g"; } if ($w =~ @fred[-1]) { print "$w]\n"; } else { print "$w,"; $g++; } }
$g is set to 0; it's my counter to track how many IPs I've iterated thru. The plan is at $g == 20, close the current chunk of IPs, and start a new chunk (setting $g back to 0). $c is the chunk counter; it counts how many chunks I have. Each chunk is named from a combination of a user-supplied variable name (gotten earlier in the program) and $c. Both $g and $c are set to 0 before the foreach begins. If $g is neither 0, it outputs $w (the IP address). If $w is the last element in the array, the current chunk is closed and the foreach stops.

The problem is that the first IF statement (which checks for $g == 0) is only run thru once, at the very beginning. I've outputted the values of both $g and $c after each foreach iteration, and it'll clearly say that $g = 0 after the 20th IP, then proceed to apparently fail the $g == 0 test. Since I know that 0 == 0, I'm assuming that it must be some kind of integer vs string confusion in my code.

2005-03-17 Edited by Arunbear: Changed title from 'have no idea why this isn't working', as per Monastery guidelines


In reply to Breaking up a list of IP addresses for Snort by Chris_LSU

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.