?
Solved

Perl hash

Posted on 2009-02-12
9
Medium Priority
?
449 Views
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?
0
Comment
Question by:Codeit1978
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 23625054
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
 
LVL 1

Author Comment

by:Codeit1978
ID: 23625323
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
 
LVL 39

Expert Comment

by:Adam314
ID: 23625513
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:Codeit1978
ID: 23625629
Let me test again to verify.
0
 
LVL 85

Expert Comment

by:ozo
ID: 23628100
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
 
LVL 6

Expert Comment

by:Fero45
ID: 23628515
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
 
LVL 85

Expert Comment

by:ozo
ID: 23628601
perhaps...  unless there could be } { in
:(.*)\):
0
 
LVL 6

Expert Comment

by:Fero45
ID: 23629079
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
 
LVL 1

Accepted Solution

by:
Codeit1978 earned 0 total points
ID: 32662561
Thanks i figured it out myself.
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question