Use an array reference as a switch to point to whichever array is appropriate depending on the section title. Then there is no need to back up a line.

use strict; use warnings; use feature qw{ say }; open my $inFH, q{<}, \ <<__EOD__ or die $!; Relay access denied (total: 5) 1 46.183.217.174 1 46.183.220.137 1 46.183.220.138 1 46.183.220.139 1 46.183.223.239 Sender address rejected: Access denied (total: 5) 1 47.183.223.239 1 219.38.243.204 1 183.29.8.198 1 183.29.9.133 1 183.29.9.135 Relay access denied (total: 8) 1 218.38.243.204 1 185.29.8.198 1 185.29.9.133 1 185.29.9.135 1 46.183.217.162 1 46.183.217.165 1 46.183.217.169 1 91.236.75.169 __EOD__ my @rad; my @sad; my $raSwitch; while ( <$inFH> ) { if ( m{\s+\d+\s+(\S+)} ) { push @{ $raSwitch }, $1; } elsif ( m{Relay access denied} ) { $raSwitch = \ @rad; } elsif ( m{Sender address rejected} ) { $raSwitch = \ @sad; } else { warn qq{Line not recognised: $_}; } } say q{Relay access denied:}; say qq{ $_} for @rad; say q{Sender address rejected:}; say qq{ $_} for @sad;

The output.

Relay access denied: 46.183.217.174 46.183.220.137 46.183.220.138 46.183.220.139 46.183.223.239 218.38.243.204 185.29.8.198 185.29.9.133 185.29.9.135 46.183.217.162 46.183.217.165 46.183.217.169 91.236.75.169 Sender address rejected: 47.183.223.239 219.38.243.204 183.29.8.198 183.29.9.133 183.29.9.135

I hope this is helpful.

Cheers,

JohnGG


In reply to Re: Loop problem: jumps one record by johngg
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.