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


In reply to Re^5: Loop problem: jumps one record by haukex
in thread Loop problem: jumps one record by Anonymous Monk

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.