http://www.perlmonks.org?node_id=1004607


in reply to Re: Counting keys in a hash
in thread Counting keys in a hash

Thank you NetWallah, that is what I was looking for

I have put your code in but I'm not getting a match with the regular expression (it's saying uninitialized value in pattern match m//)... it's probably because the data I had for you to look at is not exact. I am at work right now, so let me post the exact data below...

----DATA---- Oct 17 10:35:35 esw110tf222 ssg5-serial: NetScreen device id=000123456 +78 [Root]system-alert-000008: IP Spoofing! From 255.255.255.255 to 0. +0.0.0, proto 1 (zone Untrust int ethernet0/4). Occurred 1 times. (201 +2-1017 10:35:35) Oct 17 10:35:35 esw110tf222 ssg5-serial: NetScreen device id=000123456 +78 [Root]system-alert-000008: IP Spoofing! From 255.255.255.255 to 0. +0.0.0, proto 1 (zone Untrust int ethernet0/4). Occurred 1 times. (201 +2-1017 10:35:35)

I tried my own regex:

m/\](.*)\(20/;
But it still throws the same error...

I think this is really close now... can you help fix it for that data?

Replies are listed 'Best First'.
Re^3: Counting keys in a hash
by NetWallah (Canon) on Nov 19, 2012 at 20:34 UTC
    Change the line to :
    next unless my ($date,$time, $device, $err) = /(\w+ \d+) ([\d:]+)\s +(\S+).+\[[^\]]+\]([^\(]+)/;
    This is a LOT more forgiving than the original, and allows your new data to pass through, producing:
    DEVICE --> esw110tf222 ============== DATE --> Oct 17 TIME --> 10:35:35 ERROR --> system-alert-000008:IP Spoofing! From 255.255.255.255 to 0 +.0.0.0, proto 1 The above error occurred 2 times

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

      I'm sorry to keep bugging you NetWallah, but I'm still getting the uninitialized value error.

      I can't figure out why its still giving me the error. I have my original text file with all of the data dumped into an array and my code looks like this:

      while (@log) { <code here> }

      The array @log contains my data. The loop will still work if the data is in an array, won't it? When the code is run, it continually throws the uninitialized value error until I Ctrl^C to stop it. It never leaves the loop.

      I'm going to continue to work on it because your output looks exactly how I need it to look... If you have anymore suggestions let me know.

      UPDATE: Its working now... I had to change the while to a foreach because it is an array and not a filehandle... I may be able to figure this out by myself, but is there a way to make it display the device on every output instead of just the first one?

        The loop will still work if the data is in an array, won't it?

        No, the diamond operator <> is special. In the original loop:

        while (<DATA>) {

        one line is read on each iteration, and the loop continues until <DATA> returns undef to signal that the data accessed via the filehandle (DATA in this case) has all been read in.

        But if you have your data in an array (presumably one line per element), you need a for loop (also spelled foreach — they’re the same):

        for (@log) { <code here> }

        which iterates over the array until each element has been processed.

        (Note that in both loops, the line/element read in on a single iteration is stored in the special variable $_.)

        Hope that helps,

        Athanasius <°(((><contra mundum