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

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a list of servers with their status in a file. I need to get the last occurence of each of the servers in the file. The server names are VISHU, VISHU2, JITHU, JITHU2, MADHU, MADHU2
The file format is :
VISHU: server running 12/3/99
VISHU2: server down 12/3/99
VISHU :server running 3/12/00
JITHU : server down 3/12/00
JITHU2: server running 4/12/00
......

I need to find the last occurences of each of the servers.

Originally posted as a Categorized Question.

  • Comment on How do I get the last occurrence of a word (or words) in a file?

Replies are listed 'Best First'.
Re: How do I get the last occurrance of a word (or words) in a file?
by merlyn (Sage) on May 11, 2000 at 02:13 UTC
    By getting the "last" occurrance, I don't know if you want to know the line it's on (as a line number), or the contents of the line. So I'll do both:
    while (<>) { if /^((VIS|JIT|MAD)HU2?):/) { $found{$1} = [$., $_]; } } for (sort keys %found) { print "$_ => @{$found{$_}}\n"; }
    season to taste.
Re: How do I get the last occurrence of a word (or words) in a file?
by mslattery (Initiate) on Apr 19, 2002 at 18:50 UTC
    Or stated another way ... arn't hashes great?

    while (<>) { /^(.+)\:/ ; # Capture Server Name $hash{$1} = $_ ; # Create Hash Key if found } foreach $server (sort keys %hash) { print "$hash{$server}\n" ; }
    Just for another style!

    Have fun,
    mslattery