Perl hash

Hello, I have a perl script that grep though a log file and if the regular expression matches it add's it to the hash.  It would appear that it's only adding the first instance to the hash.

What I would like to do is have it add the last found instance that matches the reg expression to the hash.

Can this be accomplished?
LVL 1
Codeit1978Asked:
Who is Participating?
 
Codeit1978Connect With a Mentor Author Commented:
Thanks i figured it out myself.
0
 
Adam314Commented:
Yes, but you'll have to provide more info.  A sample of your log, and what you want would be helpful.

For a hash, a given key can have only 1 value (this value can be a reference to an array that contains multiple values...).  So if you want the last match, you could just set the hash with that key to the value.  Any previous value will be thrown away, and the new value stored.
0
 
Codeit1978Author Commented:
Hey, sorry, here is a snip of th elogs.  So I have an if statement that if the reg expression meets add it to the hash.   So as it, it only stores the first match, I would like it to ignor the first and only store the last.

my %user;
if(/\((\d+)\/(\d+)? (\d+):(\d+):(.*)\):\{.*\} \{(.*)/)
                                   {
                                    $user{full} = $6;
                                   }

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Adam314Commented:
With what you've shown, it will update the hash every time it matches.  So after looping through a file, it will store the last match - unless there is something in your loop going through the logs that causes it to stop processing.  You'll have to post more of your code, and maybe a log.
0
 
Codeit1978Author Commented:
Let me test again to verify.
0
 
ozoCommented:
are there more than one matches on a single string?
a regex on a string will match at the first place it can in that string
to get the last match in a string you can do
/[.\n]*\((\d+)\/(\d+)? (\d+):(\d+):(.*)\):\{.*\} \{(.*)/
0
 
Fero45Commented:
Since none of your five matches is preceded by \} \{, you could perhaps leave out the beginning of the regex string and use only the end, e.g.

  if ( /\} \{(.*)/ )     {
      $user{full} = $1;
  }

0
 
ozoCommented:
perhaps...  unless there could be } { in
:(.*)\):
0
 
Fero45Commented:
Yeah, that is true, then this could work
IF     /(.*)\):\{.*\} \{(.*)/

... again unless a similar combination follows somewhere in the string.
The author should paste the string.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.