Here's the code bit that parses thru the array of IPs (@fred) and outputs them:
$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.$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++; } }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |