Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Counting keys in a hash

by NetWallah (Abbot)
on Nov 19, 2012 at 20:34 UTC ( #1004612=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re^3: Counting keys in a hash
Select or Download Code
Re^4: Counting keys in a hash
by cspctec (Sexton) on Nov 20, 2012 at 14:05 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004612]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-08-01 02:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls