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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:42:46+00:00 2026-05-28T13:42:46+00:00

In my perl progarm i have the following code: @alpha=(‘toM’,’jERRy’,’mickeY’,’MARio’); print sort{$a cmp $b}

  • 0

In my perl progarm i have the following code:

@alpha=('toM','jERRy','mickeY','MARio');
print sort{$a cmp $b} @alpha,'  "***MIDDLE STRING***"  ';

here it prints the

‘ “***MIDDLE STRING***” ‘

first and then the sorted @alpha list. Why it prints in reverse order, when

print "a","b";

prints

ab

in correct order. I googled for help but it speaks about “reverse” function/method of perl but i don’t use that function in my code. I found the answers irrelevant.

I guess it deals something with internal working of “print” function, using “stack” data structure, but I have no idea. Somebody help me out.

Thanks in advance…

  • 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-28T13:42:47+00:00Added an answer on May 28, 2026 at 1:42 pm
    print sort {$a cmp $b} @alpha,'  "***MIDDLE STRING***"  ';      #1
    print sort {$a cmp $b} (@alpha,'  "***MIDDLE STRING***"  ');    #2
    
    print (sort {$a cmp $b} @alpha), '  "***MIDDLE STRING***"  ';   #3
    (print sort {$a cmp $b} @alpha), '  "***MIDDLE STRING***"  ';   #4
    
    print ((sort {$a cmp $b} @alpha), '  "***MIDDLE STRING***"  '); #5
    
    print sort {$a cmp $b} @alpha;                                  #6
    print '  "***MIDDLE STRING***"  ';
    
    print sort(@alpha), '  "***MIDDLE STRING***"  ';                #7
    print sort({$a cmp $b} @alpha), '  "***MIDDLE STRING***"  ';    #8
    

    #1 and #2 are equivalent: sort is operating on a list containing all of the elements of @alpha plus the string, thanks to Perl’s automatic flattening of lists. You might be tempted to add some parentheses around sort (#3), but that doesn’t help you either, because it’s interpreted as a list of two elements (#4): the result of calling print, and the string. Thus the string will not be printed, because it has nothing to do with print.

    You could use an extra set of parentheses (#5) or split it up (#6), but knowing sort {$a cmp $b} @alpha is identical to sort @alpha, you can omit the block and the parentheses become straightforward (#7). And by the same rule, if you do need a comparison function, you can put both arguments to sort within parentheses, not separated by a comma (#8).

    I also like to define subs for different kinds of sorting, to make the code simpler to read:

    sub numerically { $a <=> $b }
    sub backward { $b cmp $a }
    
    print sort(numerically @array);    # Sort numerically ascending.
    print sort(backward @array);       # Sort lexicographically descending.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following silly Perl program: $firstarg = $ARGV[0]; print $firstarg; $input = <>;
I have the following, simplest Perl CGI script: use strict; use warnings; use CGI();
Let's say I have the following simple script: print ID : ; $ID =
I have written the following program for automating scp command in perl. #!/usr/bin/expect spawn
I have a ClearQuest API script written in Perl. If I do this: #print
I have the following Perl script. I am trying to run it in Windows
I have the following code in my C++ program: query_state = mysql_query(connection, select *
The following code is a test to test what I have already done with
I have some code that I've managed to narrow down to the following smallest-code
We have a Perl program to validate XML which is invoked from a Java

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.