PapaGeek
User
Feb 22, 2014, 9:47 PM
Post #6 of 8
(2697 views)

Re: [Kenosis] Can SPLICE be used to sort an array of hash references?
[In reply to]

Can't Post


My special thanks to all. Here is the final code that solves the problem. I also added a feature where if the rank of multiple hashes will be the same when the percentage value remains the same.
use Modern::Perl '2013'; my %dataHash; my @hashArray; print "Build the random array with all ranks = 0\n"; insertHash(15, 19, 48, 25, 25, "PPCIX"); insertHash(11, 13, 32, 17, 21, "NSEIX"); insertHash(13, 17, 44, 23, 30, "FSCRX"); insertHash(10, 14, 39, 18, 22, "POGRX"); insertHash(14, 19, 43, 20, 26, "PSSIX"); insertHash(13, 15, 37, 16, 24, "OAKIX"); print "Sort and rank array on each percentage return\n"; sortAndRank("p3m","r3m"); sortAndRank("p6m","r6m"); sortAndRank("p1y","r1y"); sortAndRank("p3y","r3y"); sortAndRank("p5y","r5y"); print "Create overall rank for each ticker symbol\n"; for (my $index = 0; $index < scalar (@hashArray); $index++) { my $hashRef = $hashArray[$index]; ${$hashRef}{rank} = ${$hashRef}{r3m} + ${$hashRef}{r6m} + ${$hashRef}{r1y} + ${$hashRef}{r3y} + ${$hashRef}{r5y} ; } print "Print results sorted by overall rank\n"; my @sorted = sort { $a>{rank} <=> $b>{rank} } @hashArray; for (my $index = 0; $index < scalar (@sorted); $index++) { my $hashRef = $sorted[$index]; foreach (sort keys %{$hashRef}) { print "$_=", ${$hashRef}{$_}, " "; } print "\n"; } sub sortAndRank { my ( $sortOn, $rank) = @_; my @sorted = sort { $b>{$sortOn} <=> $a>{$sortOn} } @hashArray; my $sizeOfArray = scalar (@hashArray); if ($sizeOfArray == 0) { return; } my $hashRef = $sorted[0]; ${$hashRef}{$rank} = 1; my $lastVal = ${$hashRef}{$sortOn}; my $lastRank = 1; for (my $index = 0; $index < $sizeOfArray; $index++) { my $hashRef = $sorted[$index]; if ($lastVal != ${$hashRef}{$sortOn}) { $lastRank = $index+1; $lastVal = ${$hashRef}{$sortOn}; } ${$hashRef}{$rank} = $lastRank; } } sub insertHash { my ($p3m,$p6m,$p1y,$p3y,$p5y,$ticker) = @_; my %dataHash; $dataHash{"p3m"} = $p3m; $dataHash{"p6m"} = $p6m; $dataHash{"p1y"} = $p1y; $dataHash{"p3y"} = $p3y; $dataHash{"p5y"} = $p5y; $dataHash{"ticker"} = $ticker; $dataHash{"r3m"} = 0; $dataHash{"r6m"} = 0; $dataHash{"r1y"} = 0; $dataHash{"r3y"} = 0; $dataHash{"r5y"} = 0; $dataHash{"rank"} = 0; push (@hashArray, \%dataHash); } and here is the output the code produces:
Build the random array with all ranks = 0 Sort and rank array on each percentage return Create overall rank for each ticker symbol Print results sorted by overall rank p1y=48 p3m=15 p3y=25 p5y=25 p6m=19 r1y=1 r3m=1 r3y=1 r5y=3 r6m=1 rank=7 ticker=PPCIX p1y=44 p3m=13 p3y=23 p5y=30 p6m=17 r1y=2 r3m=3 r3y=2 r5y=1 r6m=3 rank=11 ticker=FSCRX p1y=43 p3m=14 p3y=20 p5y=26 p6m=19 r1y=3 r3m=2 r3y=3 r5y=2 r6m=1 rank=11 ticker=PSSIX p1y=37 p3m=13 p3y=16 p5y=24 p6m=15 r1y=5 r3m=3 r3y=6 r5y=4 r6m=4 rank=22 ticker=OAKIX p1y=39 p3m=10 p3y=18 p5y=22 p6m=14 r1y=4 r3m=6 r3y=4 r5y=5 r6m=5 rank=24 ticker=POGRX p1y=32 p3m=11 p3y=17 p5y=21 p6m=13 r1y=6 r3m=5 r3y=5 r5y=6 r6m=6 rank=28 ticker=NSEIX Again, thanks to all for your help with this coding question.
(This post was edited by PapaGeek on Feb 22, 2014, 9:49 PM)
