in reply to Tagging the last elements
The data is read into an AoA and also the "ordinal" number before the combination of the "name and code" is also read into a hash table, a HoA with ordinal numbers. "ordinal number" here is a misnomer and is an artifact of first approach to the code. I expect the OP to rename things.
As it turns out, only the combination of the name and code at the end of line is unique. flintoff has JJ113952 and peter also has JJ113952.
The ordinal numbers for each combination of name/code are sorted. I also showed how to sort the original DB with a "weird sort order", but I'm not sure this is necessary and of course this can be modified to get all kinds of orders.
To print the data, if the name/code como is unique, this is a singleton. To print "end" token, there are "gaps" in what I called the ordinal numbers, so I use the maximum one what was seen. This is the -1 index.
#!/usr/bin/perl -w use strict; use Data::Dumper; my @DB; my %hash; while (<DATA>) { my($n1,$n2,$letter,$ordinal,$name,$code) = (my @record = split(/[\s,]+/,$_)); push (@DB, [@record]); push (@{$hash{"$name$code"}},$ordinal); } foreach my $name_code (keys %hash) { @{$hash{$name_code}}= sort{$a<=>$b} @{$hash{$name_code}}; } @DB = sort by_weird_sort_order @DB; #optional sort foreach my $line (@DB) { my($n1,$n2,$letter,$ordinal,$name,$code) = @$line; $ordinal = "single" if @{$hash{"$name$code"}} == 1; $ordinal = "end" if ( @{$hash{"$name$code"}}>1 and (@{$hash{"$name$code"}})[-1] == $ordinal ); printf STDOUT ("%-7s %-7s %-3s %s,%s,%s\n", $n1,$n2,$letter,$ordinal,$name,$code); } sub by_weird_sort_order { my ($a_ordinal,$a_name) = (@$a)[3,4]; my ($b_ordinal,$b_name) = (@$b)[3,4]; $a_name cmp $b_name or $b_ordinal <=> $a_ordinal } #PRINTS: #62556 63635 y single,andrew,JJ113954 #126185 126699 s single,austin,JJ113956 #441474 441538 b end,catherine,JJ029490 #442666 442843 9 8,catherine,JJ029490 #445778 445905 0 7,catherine,JJ029490 #446059 446273 l 6,catherine,JJ029490 #450319 450379 f 5,catherine,JJ029490 #81099 81630 y single,flintoff,JJ113952 #68766 69005 j end,morgan,JJ113955 #63868 63897 h 1,morgan,JJ113955 #158146 158367 i end,peter,JJ113952 #135588 136297 8 2,peter,JJ113952 #135356 135449 3 1,peter,JJ113952 __DATA__ 62556 63635 y 1,andrew,JJ113954 63868 63897 h 1,morgan,JJ113955 68766 69005 j 2,morgan,JJ113955 81099 81630 y 1,flintoff,JJ113952 126185 126699 s 1,austin,JJ113956 135356 135449 3 1,peter,JJ113952 135588 136297 8 2,peter,JJ113952 158146 158367 i 3,peter,JJ113952 441474 441538 b 9,catherine,JJ029490 442666 442843 9 8,catherine,JJ029490 445778 445905 0 7,catherine,JJ029490 446059 446273 l 6,catherine,JJ029490 450319 450379 f 5,catherine,JJ029490
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
New Problem
by crochunter (Novice) on Jul 28, 2009 at 12:38 UTC | |
by roboticus (Chancellor) on Jul 28, 2009 at 12:55 UTC | |
by graff (Chancellor) on Jul 28, 2009 at 13:08 UTC | |
by Marshall (Canon) on Jul 30, 2009 at 04:32 UTC | |
by graff (Chancellor) on Jul 30, 2009 at 04:47 UTC | |
by Marshall (Canon) on Jul 30, 2009 at 04:50 UTC |