As your data looks to be time sequential something easy like this would probably do you fine. I have given a simple way to do it with two hashes or a more perlish way with a hash of arrays. If your data lines are not ordered by time then a check to compare two time stamps will be needed for each line to decide if to update first or last time.

#!/usr/opt/perl5/bin/perl use strict; use warnings; use Data::Dumper; my %earliest; my %latest; my %hash_of_arrays; my $line; while ($line = <DATA>) { chomp $line; # remove return my ($key, $date) = split /\s+/, $line, 2; # split on space into ma +x 2 parts next unless $date; # ignore blank or malfor +med lines unless (defined $earliest{$key}) { $earliest{$key} = $date; } $latest{$key} = $date; # or with a hash of arrays $hash_of_arrays{$key} or $hash_of_arrays{$key}[0]=$date; $hash_of_arrays{$key}[1]=$date; } print Data::Dumper->Dump([\%earliest], ['%earliest']) ; print Data::Dumper->Dump([\%latest], ['%latest']); print Data::Dumper->Dump([\%hash_of_arrays], ['%hash_of_arrays']); print "first time for KEY1 was: $hash_of_arrays{KEY1}[0]\n"; print "last time for KEY1 was: $hash_of_arrays{KEY1}[1]\n"; __DATA__ KEY1 07/25/05 09:04:36 KEY2 07/25/05 09:04:36 KEY2 07/25/05 09:04:37 KEY2 07/25/05 09:05:49 KEY2 07/25/05 09:05:50 KEY3 07/25/05 09:05:50 KEY1 07/25/05 09:06:36 # output =>./first $%earliest = { 'KEY2' => '07/25/05 09:04:36', 'KEY1' => '07/25/05 09:04:36', 'KEY3' => '07/25/05 09:05:50' }; $%latest = { 'KEY2' => '07/25/05 09:05:50', 'KEY1' => '07/25/05 09:06:36', 'KEY3' => '07/25/05 09:05:50' }; $%hash_of_arrays = { 'KEY2' => [ '07/25/05 09:04:36', '07/25/05 09:05:50' ], 'KEY1' => [ '07/25/05 09:04:36', '07/25/05 09:06:36' ], 'KEY3' => [ '07/25/05 09:05:50', '07/25/05 09:05:50' ] }; first time for KEY1 was: 07/25/05 09:04:36 last time for KEY1 was: 07/25/05 09:06:36

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to Re^5: Perl Hash by Random_Walk
in thread Perl Hash by TechNoFear

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.