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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:30:27+00:00 2026-05-27T16:30:27+00:00

How would I go about sorting across multiple columns for the below code? Currently,

  • 0

How would I go about sorting across multiple columns for the below code?

Currently, the code:
1. Gets a @list of files in a $directory
2. Uses regex to get the $fileName, $fileLocation and $fileSize for each element in @list
3. Prints out the 3 values in (2) into 3 fixed-width columns
4. Then prints out the total number of files and directory size

I would like the output to display sorted by:
1. $fileName then
2. $fileLocation then
3. $fileSize

$directory = '/shared/tmp';
$count = 0;

@list = qx{du -ahc $directory};

printf ("%-60s %-140s %-5s\n", "Filename", "Location", "Size");

foreach(@list) {
  chop($_);                                                     # remove newline at end
  if (/^(.+?K)\s+(.+\/)(.+\.[A-Za-z0-9]{2,4})$/) {              # store lines with valid filename into new array
#    push(@files,$1);
    $fileSize = $1;
    $fileLocation = $2;
    $fileName = $3;
    if ($fileName =~ /^\./) {
      next; }
    printf ("%-60s %-140s %-5s\n", $fileName, $fileLocation, $fileSize);
    $count++;
  }
  else {
    next;
  }
}

print "Total number of files: $count\n";

$total = "$list[$#list]";
$total =~ s/^(.+?)\s.+/$1/;
print "Total directory size: $total\n";
  • 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-27T16:30:28+00:00Added an answer on May 27, 2026 at 4:30 pm

    You can specify your own sorting algorithm and give it to sort!

    • Documention: sort – perldoc.perl.org

    A sample implementation

    Push your results (in a hash reference) into an array called @entries, and use something like the below.

    my @entries;
    
    ...
    
    # inside your loop
    
      push @entries, {
        'filename' => $fileName,
        'location' => $fileLocation,
        'size'     => $fileSize
      };
    
    ...
    
    my @sorted_entries = sort {
      $a->{'filename'} cmp $b->{'filename'} || # use 'cmp' for strings
      $a->{'location'} cmp $b->{'location'} ||
      $a->{'size'}     <=> $b->{'size'}        # use '<=>' for numbers
    } @entries;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I go about sorting in descending order, a List<Tuple<int, int>> using the
I'm having an issue with sorting a list that contains a dict. Currently I
How would I go about sorting an array of nested arrays, based on the
I would like to know more about the array sorting. Like we have a
How would I go about sorting an array of NSString Objects by a custom
This is interesting, sorting links by content , but how would you go about
I assumed sorting a CSV file on multiple text/numeric fields using Python would be
How would you go about implementing a priority queue using a linked list in
Just wondering how you would go about implementing something similar to stackoverflow'd related questions.
Any ideas how I would go about writing a javascript method to insert an

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.