in reply to Re^4: Loop problem: jumps one record
in thread Loop problem: jumps one record
Hi math&ing001,
When I run your code against your sample input, it does not match the output you gave; also you didn't say what output you expect - the more precise you are in your questions, the better we are able to help :-)
What I see happening is that I get stuff like "(total:" in the output, the reason is that the regex /(\d+)\s+(\S+)/ also matches the string "blocked using dummy3 (total: 5)". One way to avoid this would be to be more specific in your regexes. One simple fix would be to write your regexes as /^\s*(\d+)\s+(\S+)\s*$/.
Anyway, the first thing I would suggest is that you don't use do { } until () loops, and instead use while loops, and then use the last command to break out of the loop under certain conditions, this will give you more precise control. For example, the first of your inner loops could be written like this:
if (/blocked.using/) { while (<>) { /^\s*(\d+)\s+(\S+)\s*$/ or last; $ip = $2; print "$ip\n"; } }
As for the state machine approach:
use warnings; use strict; use constant { IDLE => 0, RELAY => 1, BLOCKED => 2, }; my $state = IDLE; while (<>) { chomp; if (/Relay.access.denied/) { $state = RELAY; } elsif (/blocked.using/) { $state = BLOCKED; } elsif (/^\s*(\d+)\s+(\S+)\s*$/) { if ($state == RELAY) { print "Relay Access Denied: $2\n" } elsif ($state == BLOCKED) { print "Blocked: $2\n" } } else { $state = IDLE; } }
Hope this helps,
-- Hauke D
|
|---|