Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6067593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:37:29+00:00 2026-05-23T09:37:29+00:00

I have this method of sorting which is basically just basic thought processes, not

  • 0

I have this method of sorting which is basically just basic thought processes, not using Perl power, and once in a while it doesn’t act how I want it (misses some frequency counting). I was wondering if there was a better way to sort this.

Objective Sort the array based on frequency of matches found.

Sample array of arrays

##ADDED 1 to END of EACH ROW, just because my sort forced me too!!!
my @all_matches = (["chpt10_2", "sent. 2", "alice", "nsubj", "animals", "protect"],
               ["chpt12_1", "sent. 54", "bob", "nsubj", "cells", "protect"],
               ["chpt25_4", "sent. 47", "carol", "nsubj", "plants", "protect"],
               ["chpt34_1", "sent. 1", "dave", "nsubj", "cells", "protect"],
               ["chpt35_1", "sent. 2", "eli", "nsubj", "cells", "protect"],
               ["chpt38_1", "sent. 1", "fred", "nsubj", "animals", "protect"],
               ["chpt54_1", "sent. 1", "greg", "nsubj", "uticle", "protect"]
              );

Current sort

@all_matches = sort {lc($a->[4]) cmp lc($b->[4])} @all_matches;

my ($last_word, $current_word, $word_count);

for my $j (0 .. $#all_matches) {

    $current_word = $all_matches[$j][4];

    if (lc($last_word) eq lc($current_word)) {
        $word_count++;
        }
    else {
        if ($j != 0)
        {
            for (my $k = 1; $k <= $word_count; $k++)
            {
               $all_matches[($j-$k)][6] = $word_count; 
            }
        }
        $last_word = $current_word;
        $word_count = 1;
        }
}
@all_matches = sort {$b->[6] <=> $a->[6] || lc($a->[4]) cmp lc($b->[4])} @all_matches;

Problem The 6th column is set to 1 when all_matches is passed in!!! The reason this was done was because sometimes, the count ($match->[6]) was blank.

Bonus? Match frequency of times the last two columns appear together (right now I’m pretty sure it just checks 2nd last column). In this test case, the final column is all the same, in the actual case, there are different suffixes on the end (ie. protect, protects, protective etc..)

THANKS a lot for your time. I’ve tried using a hash, and thought it worked, however it neglected some things.

Here was my hash attempt. Couldn’t tell you yet why this didn’t work:

my %freq;
foreach ( map{$_->[4]}@results) #feeds in list of animals, cells, uticle, etc.
{
   $freq{lc $_}++;
}


@results = sort {$freq{lc $b->[4]} <=> $freq{lc $a->[4]} #freq order
                                   or
                         $a->[0]  cmp $b->[0]            #text col 0      
                } @results; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:37:29+00:00Added an answer on May 23, 2026 at 9:37 am

    Why not create a hash of the keys with a count of the occurrences, and use that:

    my %counts;
    foreach my $rowref (@all_matches)
    {
         $counts{lc($rowref->[4])}++;
    }
    
    @all_matches = sort { $counts{lc($b->[4])} <=> $counts{lc($a->[4])} ||
                          lc($a->[4]) cmp lc($b->[4])
                        } @all_matches;
    

    Tested…

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    my @all_matches = (
        ["chpt10_2", "sent. 2", "alice", "nsubj", "animals", "protect"],
        ["chpt12_1", "sent. 54", "bob", "nsubj", "cells", "protect"],
        ["chpt25_4", "sent. 47", "carol", "nsubj", "plants", "protect"],
        ["chpt34_1", "sent. 1", "dave", "nsubj", "cells", "protect"],
        ["chpt35_1", "sent. 2", "eli", "nsubj", "cells", "protect"],
        ["chpt38_1", "sent. 1", "fred", "nsubj", "animals", "protect"],
        ["chpt54_1", "sent. 1", "greg", "nsubj", "uticle", "protect"]
        );
    
    my %counts;
    foreach my $rowref (@all_matches)
    {
        $counts{lc($rowref->[4])}++;
    }
    
    @all_matches = sort { $counts{lc($b->[4])} <=> $counts{lc($a->[4])} ||
                          lc($a->[4]) cmp lc($b->[4])
                        } @all_matches;
    
    my $i = 0;
    foreach my $rowref (@all_matches)
    {
        $i++;
        print "$i";
        print " $_" foreach (@$rowref);
        print "\n";
    }
    

    Output:

    1 chpt12_1 sent. 54 bob nsubj cells protect
    2 chpt34_1 sent. 1 dave nsubj cells protect
    3 chpt35_1 sent. 2 eli nsubj cells protect
    4 chpt10_2 sent. 2 alice nsubj animals protect
    5 chpt38_1 sent. 1 fred nsubj animals protect
    6 chpt25_4 sent. 47 carol nsubj plants protect
    7 chpt54_1 sent. 1 greg nsubj uticle protect
    

    As noted in a comment, given the data shown, the lc operations are not needed – and removing them would improve performance, as would adding a case-converted key to each array.

    And with lc used once per row – notice the munged data values:

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    my @all_matches = (
        [ "chpt10_2", "sent. 2",  "alice", "nsubj", "animAls", "protect" ],
        [ "chpt12_1", "sent. 54", "bob",   "nsubj", "celLs",   "protect" ],
        [ "chpt25_4", "sent. 47", "carol", "nsubj", "plAnts",  "protect" ],
        [ "chpt34_1", "sent. 1",  "dave",  "nsubj", "cElls",   "protect" ],
        [ "chpt35_1", "sent. 2",  "eli",   "nsubj", "cells",   "protect" ],
        [ "chpt38_1", "sent. 1",  "fred",  "nsubj", "Animals", "protect" ],
        [ "chpt54_1", "sent. 1",  "greg",  "nsubj", "uticle",  "protect" ],
        );
    
    my %counts;
    foreach my $rowref (@all_matches)
    {
        push @$rowref, lc($rowref->[4]);
        $counts{$rowref->[6]}++;
    }
    
    @all_matches = sort { $counts{$b->[6]} <=> $counts{$a->[6]} || $a->[6] cmp $b->[6]
                        } @all_matches;
    
    my $i = 0;
    foreach my $rowref (@all_matches)
    {
        $i++;
        print "$i";
        printf " %-9s", $_ foreach (@$rowref);
        print "\n";
    }
    

    Output:

    1 chpt12_1  sent. 54  bob       nsubj     celLs     protect   cells    
    2 chpt34_1  sent. 1   dave      nsubj     cElls     protect   cells    
    3 chpt35_1  sent. 2   eli       nsubj     cells     protect   cells    
    4 chpt10_2  sent. 2   alice     nsubj     animAls   protect   animals  
    5 chpt38_1  sent. 1   fred      nsubj     Animals   protect   animals  
    6 chpt25_4  sent. 47  carol     nsubj     plAnts    protect   plants   
    7 chpt54_1  sent. 1   greg      nsubj     uticle    protect   uticle   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this method Verify_X which is called during databind for a listbox selected
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data
I have this method in my db class public function query($queryString) { if (!$this->_connected)
I have this method: public bool CanExecute() And after 70 commits, I added an
I have this method: private delegate void watcherReader(StreamReader sr); private void watchProc(StreamReader sr) {
Currently i have this method: static boolean checkDecimalPlaces(double d, int decimalPlaces){ if (d==0) return
I have this function from a plugin (from a previous post) // This method
So i have this method: internal K GetValue<T, K>(T source, string col) where T
I have this factory method in java: public static Properties getConfigFactory() throws ClassNotFoundException, IOException
Does anyone have any thoughts on this method? I have did some performance testing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.