We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Perl hash

Codeit1978 asked
Medium Priority
Last Modified: 2013-11-05
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?
Watch Question

Top Expert 2009

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.


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

Top Expert 2009

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.


Let me test again to verify.
Most Valuable Expert 2014
Top Expert 2015

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+):(.*)\):\{.*\} \{(.*)/

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;

Most Valuable Expert 2014
Top Expert 2015

perhaps...  unless there could be } { in

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.
Thanks i figured it out myself.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.