in reply to Breaking up a list of IP addresses for Snort

Two problems jump out at me:
  1. The test for if $w is the last one in the list is written incorrectly. It should be
    if ($w eq $fred[-1])
  2. The second if block should skip the remainder of the loop. That is, the very last if/else in the loop should not occur if $g == 20.
That said, I think you're going about this the hard way. In addition to the splice() approach recommended above, you can iterate through the list in blocks of 20, like so:
for ( my $chunk = 0; $chunk < @fred; $chunk += 20 ) { print "var $varname $chunk [\n"; print "$fred[$chunk]"; for my $ofs ( 1 .. 19 ) { print ",$fred[$chunk+$ofs]"; } print "]\n"; }
However, even that looks a bit messy. How about
for ( my $chunk = 0; $chunk < @fred; $chunk += 20 ) { local $" = ','; print "var $varname $chunk [@{[ map { $fred[ $chunk+$_] || () } 0 +.. 19 ]}]\n"; }
This deviates from your model by having the chunks labeled 0, 20, 40..., rather than 0, 1, 2... If that part is important, you could fix it by doing (e.g.)
print "var $varname ", $chunk/20, " . . .