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 8275523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:06:08+00:00 2026-06-08T08:06:08+00:00

I need a Perl comparison function that can be used with sort. Each key

  • 0

I need a Perl comparison function that can be used with sort.

Each key is a text string that has an arbitrary number of subkeys, separated by delimiter characters (dot, colon, space, and slash). Some subkeys are numeric, and need to be sorted numerically. The key format and number of subkeys varies. Therefore, the comparison has to handle one key being longer than the other, and has to handle the case where a subkey is numeric in one key but not in another (in which case a textual comparison is appropriate for that subkey).

This works, but I bet there are better solutions:

use warnings;
use strict;
use Scalar::Util qw[looks_like_number];

sub hier_cmp {

    my $aa = $a;
    my $bb = $b;

    # convert all delims (. : / space) to the same delim

    $aa =~ tr/.:\/ /::::/;
    $bb =~ tr/.:\/ /::::/;
    my @lista = split(":", $aa);
    my @listb = split(":", $bb);

    my $result;

    for my $ix (0 .. min($#lista, $#listb)) {
        if (exists($lista[$ix]) && exists($listb[$ix])) {
            if ( looks_like_number($lista[$ix]) && looks_like_number($listb[$ix])) {
                # compare numerically
                $result = ($lista[$ix] <=> $listb[$ix]);
            } else {
                # compare as strings
                $result = ($lista[$ix] cmp $listb[$ix]);
            }
            if ($result == 0) {
                next;
            }
            return $result;

        } elsif (exists($lista[$ix])) {
            return 1;
        } else {
            return -1;
        }
    }
}

For my purposes, readability is more important than speed. This is just for an internal tool, and lists will rarely have more than hundreds of elements. However, any opportunity to learn something is good.

As you can see, I’m not a perl wizard. Even trivial improvements on my code would be appreciated.

Thanks!

  • 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-06-08T08:06:09+00:00Added an answer on June 8, 2026 at 8:06 am

    It would help if you gave us some data to test with, but this code passes a few basic tests and it looks right.

    It simplifies the problem by using the List::MoreUtils function pairwise to create an array of field pairs.

    Then it is just a matter of checking whether only one is defined, when one of the lists has come to an end before the other and should be sorted first; if they are both numeric, when they should be compared with a numeric comparison; or otherwise simply compare them as strings.

    If the end of the array of pairs is reached then everything has matched and zero is returned to indicate equiality.

    Update

    I have changed this code to remove the dependency on List::MoreUtils::pairwise.

    use strict;
    use warnings;
    
    use Scalar::Util 'looks_like_number';
    
    sub hier_cmp {
    
      our ($a, $b);
    
      my @a = split m|[.: /]+|, $a;
      my @b = split m|[.: /]+|, $b;
    
      for my $i (0 .. $#a > $#b ? $#a : $#b) {
        my @ab = ( $a[$i], $b[$i] );
        if (grep defined, @ab < 2) {
          return defined $ab[0] ? 1 : -1;
        }
        else {
          my $numeric = grep(looks_like_number($_), @ab) == 2;
          my $result = $numeric ? $ab[0] <=> $ab[1] : $ab[0] cmp $ab[1];
          return $result if $result;
        }
      }
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a perl program that accept command lines arguments which can
I need to get a string in perl whose length is varying each day.
I need the Perl regex to parse plain text input and convert all links
I need a Perl script to spawn itself with an arbitrary name, i.e. so
I need to create Perl code which allows counting paragraphs in text files. I
In a shell-script (or Perl) I need a subroutine that returns for all my
I need to write a Perl script that pipes input into a Java program.
I'm looking for a performance comparison between perl and boost regular expression. I need
I need a PCRE(Perl Compatible Regular Expression) that will match all non -images(jpg,png,tiff) from
I need the Python analog for this Perl string: unpack(nNccH*, string_val) I need the

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.