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

  • Home
  • SEARCH
  • 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 4054920
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:38:11+00:00 2026-05-20T14:38:11+00:00

I’m having some trouble with subroutines, namely the sort function being used to sort

  • 0

I’m having some trouble with subroutines, namely the sort function being used to sort an array of numbers. I know if you use the sort function solely, it sorts using ASCII format, as the book describes this and that sorts in not the desired order. I’m aware and have an understanding of what gets returned by using <=> to compare values, even being introdued to cmp for strings (although I have not used it yet).

What I don’t understand specifically is how it sorts them numerically – that actual process. I understand what is returned, but the book just says it returns -1, 0 and 1 and not how specifically the numbers end up finally sorted to 1 8 24 72 144 288.

My example:

sub sort_by_number {
    return $a <=> $b;
}

@myArray = (1,24,8,144,72,288);

foreach(sort sort_by_number(@myArray)) {
    print("$_ ");
}

The example I understand fully. It makes perfect sense but I think that’s mainly due to the coding:

#!/usr/bin/perl

print("Please enter your name: ");
$name = <STDIN>;
chomp($name);
print("Please enter your age: ");
$age = <STDIN>;
chomp($age);

print(greeting($name, $age));

sub greeting {
    $msg = "Hello $_[0], ";
    determine_age($_[1],$msg);
}

sub determine_age {
    $num = ($_[0] <=> 18);
    if ($num == -1) {
        return "$_[1]you are under 18.($_[0])\n";
    } elsif ($num == 0) {
        return "$_[1]you will be a 19 on your next birthday!\n";
    } else {
        return "$_[1]you are over 18!($_[0])\n";
    }
}

Massively appreciative if someone could clarify.

  • 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-20T14:38:12+00:00Added an answer on May 20, 2026 at 2:38 pm

    Your second example doesn’t do any sorting.

    To answer “how it sorts them numerically – that actual process?“, Perl internally implements sort using Merge Sort algorithm (was quicksort before Perl 5.6).

    The algorithm itself is fairly complicated (See Wiki for details), BUT underneath it ends up comparing 2 numbers and deciding if one is bigger than the other, and performing some action depending on that decision. If you’re curious about details, the algorithm part that needs the comparison is the if first(left) ≤ first(right) line on the Wiki example.

    This is where the custom sort subroutine comes in – it answer “which number is bigger” question for the sorting algorithm (or, to be more specific, whether one number is less than or equal than another).

    The way this is implemented, Perl’s sort will internally call the “comparator” function, and pass it 2 arguments (by aliases $a and $b); and expect the function to return negative, zero or positive if the first is less than, equal or greater than the second one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.