in reply to Re: Sort array using a ranking system from separate hash
in thread Sort array using a ranking system from separate hash

Thank you Rolf. I am looking to order the elements of, say, a single array according to a list determined held separately. So, if my original array was ordered

@array = (ATVELLSFL, QLFHLCLII, YLVSFGVWI, MQLFHLCLI);

and my order preference is

MQLFHLCLI FLPSDFFPS YLVSFGVWI LLWFHISCL QLFHLCLII IISCSCPTV ELMNLATWV HISCLTFGR ATVELLSFL

then my array should be re-ordered

@orderedArray = (MQLFHLCLI, YLVSFGVWI, QLFHLCLII, ATVELLSFL);

The length of the array to ordered will always be shorter than the my order preference list, because this list is used with a large number of different arrays

Apologies for not explaining my question properly in the first place

Replies are listed 'Best First'.
Re^3: Sort array using a ranking system from separate hash
by haukex (Archbishop) on Aug 08, 2016 at 10:41 UTC

    Hi Sandy_Bio_Perl,

    Apologies for not explaining my question properly in the first place

    I recommend you read How do I post a question effectively? and Short, Self Contained, Correct Example. Showing your own coding efforts not only gives people trying to help a basis to start from, it also shows that you're not just trying to abuse PerlMonks as a code writing service. Not only that, boiling down your code into a SSCCE and trying to explain the problem will often help you yourself to figure out the issue at hand.

    Anyway, BrowserUk and Marshall already answered your question and provided code; the only difference is that instead of their %freq resp. %histo hashes being populated from the data, you simply need to pre-populate it.

    Hope this helps,
    -- Hauke D

      Sorry, I didn't include all my code, because it is long and not very good!

      Just in case you think I am not trying to do it myself see the attached

      #!/usr/bin/perl use strict; use warnings; use HBV; my $machine="PC"; my ($inputDataPath, $outputDataPath,$inPathForScript,$outPathForScript +) = HBV::PathFinder($machine); my $origfile = $outputDataPath. qw(individualAlleles\HLA-A-1_HBe_Analy +sis.txt); # HLA-A-1_HBe_Analysis.txt open (IN, $origfile) or die "cannot open $origfile"; my @columns; my $count = 0; my @genotypeAndAllele; my @binders; my @lineCount; my $bitBinder; my @bitBinder; my @copybinders; while (my $line = <IN>){ chomp $line; if ($line =~/^([A-Z]{9}).*(IOH.{7}).*([1-9].?).*(HLA.{8})/){ push @genotypeAndAllele,$4.' '.$2; push @lineCount,$3; } if ($line =~/^([A-Z]{9})/){ push @binders, $1; @copybinders = @binders; } } foreach my $el (@lineCount){ for (my $i=0; $i <$el; $i++){ my $bit = $binders[$i]; $bitBinder .= $bit." "; } push @bitBinder, $bitBinder; $bitBinder=''; splice @binders,0,$el; } my %alleleProtein; foreach my $item (@genotypeAndAllele){ $alleleProtein{$item}++; } my %bindersHash; foreach my $item (@copybinders){ $bindersHash{$item}++; } my %genoBinderAlleleHash; @genoBinderAlleleHash{@genotypeAndAllele} = +@bitBinder; foreach my $keys (sort {$a cmp $b} keys (%genoBinderAlleleHash)){ print "$keys \($alleleProtein{$keys}\) = $genoBinderAlleleHash{$keys}\ +n"; } foreach my $keys (sort {$bindersHash{$b} <=> $bindersHash{$a}} (keys ( +%bindersHash))){ print "$keys = $bindersHash{$keys}\n"; }

      Sorry to have bothered you

Re^3: Sort array using a ranking system from separate hash
by LanX (Saint) on Aug 08, 2016 at 10:36 UTC
    Isn't it straight forward after reading sort ?

    like modifying this snippet from the perldoc accordingly?

    # this sorts the %age hash by value instead of key # using an in-line function my @eldest = sort { $age{$b} <=> $age{$a} } keys %age;

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!