Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lost in how to return last 3 occurances per staff 1

Status
Not open for further replies.

Maldini

Technical User
Nov 10, 2002
55
GB
Hi,
Having yet another problem which has left me stuck totally...

I have to find a way to return the last three occurances (in terms of nearest date) of a person's name in a particular file (already read into an array) and then ouput this to another file...

Say this is part of my file...

10.01.2003 Sales Jason Jones 24.5
12.01.2003 HR Joey Robert 33.0
02.01.2003 Sales Jason Jones 14.5
17.01.2003 Sales Jason Jones 20.0
28.01.2003 Sales Jason Jones 28.0
10.01.2003 HR Joey Robert 13.0
15.02.2003 HR Joey Robert 38.0
01.02.2003 HR Joey Robert 23.5

The output I need is:
28.01.2003 Sales Jason Jones 28.0
17.01.2003 Sales Jason Jones 20.0
10.01.2003 Sales Jason Jones 24.5
15.02.2003 HR Joey Robert 38.0
01.02.2003 HR Joey Robert 23.5
12.01.2003 HR Joey Robert 33.0

Basically its not only a generalized return of last three recrods, but also by personname as well..

So far I've managed transform the dates into epoch format for comparison and that about it

Code:
m/^(\d\d)\.(\d\d)\.(\d\d\d\d)\ /;			   my $mday = $1;			
my $mon = $2;
my $year = $3;
my $date_var = timelocal(0,0,0,$mday,$mon-1,$year);

I've been thinking of hashes but then I have one key (date) to sort by but at the same time must handle the case differenty per person

I'm simply lost as to how to approach the rest of this problem...

Thanks,
Maldini.
 
I think a hash is the best way to go, and actually a hash of hashes. The hash keyed by name and each name points to a hash of dates with the whole record for each date as the value for each date key. This way you store all the lines and then sort them at the end when you go to print them.

The other option is to have the name key point to an array that holds the latest three dates. This requires more on the loading end but you only store three lines for each name rather than all the lines.

Which you choose depends on the makeup of the data and the limitations of your computer.

Here is an example implementation of the first way:
Code:
#Load data into %names hash
while (<DATA>) {

    my ($date, undef, $last, $first, undef) = split;
    #format date so that it can be compared numerically
    my $date_var = join '', (split /\./, $date)[2,1,0];
    $names{$last.$first}{$date_var} = $_;
}
# sort and print the latest three
for my $name (sort keys %names) {

    # Sort by date and loop through first three values
    for ( (sort { $b <=> $a } keys %{$names{$name}})[0..2] ) {

        print $names{$name}{$_};
    }
}
jaa
 
Thanks for the idea.. I think this is the way to go...

but just one question... is there a wqay to modify this so that it works no matter how many names a person may have?
Say like some ppl have two middle names and the initials are also stored in the file...
 
oops...solved it now, thanks again justice41 :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top