The line break in your regexp is throwing off your match. If you make that a single line it ends up getting matches. What you end up with is a hash where the keys are "Service ..." all the way to the end of the text block and the values are "Host: <ip>" strings. So your keys are multi-line blocks of text and your values are strings that have IP's. That doesn't seem to be what you'd really want here but maybe it's a start.

You can also put that $/ setting once outside the loop. I find it good practice to local those but it doesn't matter much for a standalone piece of code.

I tested this version:

use strict; use warnings; my $file = 'input.txt'; my (%hash, @ips, @alerts); open (FILE, "$file") or die "Can't open $file\n"; local $/ = '-------------------------------------------------'; while (<FILE>){ $hash{$2}{$1} = 0 if (/(Host: \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*(Service:.*Sever +ity:.*)/ms); } foreach my $key (keys %hash) { $hash{$key} = [keys %{$hash{$key}}]; } my ($key, $values); while (($key, $values) = each %hash) { print "$key --> ", join('|', @$values), "\n"; }


In reply to Re: Building a Hash with Multiple Values by steves
in thread Building a Hash with Multiple Values by Dru

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.