Sorting problem....

Posted on 2003-02-21
Medium Priority
Last Modified: 2010-05-18
Is there anyway to sort a textfile by two fields that are delimited by different things?

like the following....
field 1: field 2 (field 3)

Server a: 150 (85)
Server b: 20
Server c: 150 (70)
Server d: 75

I wanted to sort this somehow first by the second field, and then when there is the clash of the second field by the third field (which only exists is there is a field 2 clash)...

problem is i have no idea of how i can handle this situation where i have the : as the delimiter between field 1 and 2 and then the space as delimiter between fields 2 and 3....

PS. sort meaning sorted in numeric order so 20 appears higher up than 150

Many thanks in advance.
Question by:Maldini
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 7995776

Here's one way to do it:

  my @data = (
    'Server a: 150 (85)',
    'Server b: 20',
    'Server c: 150 (70)',
    'Server d: 75'

  my @list_of_hashes;
  foreach ( @data ) {
    push @list_of_hashes, {
      field_1 => $1,
      field_2 => $2,
      field_3 => $3

  my @sorted_data = sort map {
    "$_->{ field_1 }: $_->{ field_2 }" .
    $_->{ field_3 } ? " ($_->{ field_3)" : '';

  } sort {
    $a->{ field_2 } <=> $b->{ field_2 } ||
    $a->{ field_3 } <=> $b->{ field_3 }

  } @list_of_hashes;

So, that's the code. Here's what's going on:

I'm assuming the data is in the above format as you've shown in some file somewhere and that you'll slurp it up into an array, like the one I declared above. The strategy I've chosen is to break the data up into fields, just like you've described and to do it by using the capturing features of a regular expression. It's an hairy one; if you can get your hands on the "Hip Owl" book by Jeff Friedl (Mastering Regular Expressions, O'Reilly press), it's very useful.


This portion says, "Capture any non-colon character from the beginning of the line until you see a colon followed by a whitespace character."


This is a pretty useful and regularly used one, "capture at least one digit".


This one uses the non-capturing parenthesis and the capturing parenthesis and just plain parenthesis. It says, "Capture at least one digit that is enclosed in a pair of parenthesis and is preceeded by a space; the whole construct may or may not be around to capture, so don't fail any other portions of the regular expression just because you don't see this construct."

From then on, its a matter of assigning the captured data to the fields and then sorting a list of hashes. The map reconstructs the data from each hash so that it looks just like the input data.

Hope this helps,

- m.

Author Comment

ID: 7995932
Thanks for the reply.

Just one question, printing the @sorted_data only seems to return the sorted third field with the first and second field not shown at all.
Is there any way to change to so that all fields are shown?

Thanks again. :)

Accepted Solution

mikezone earned 140 total points
ID: 7996471
Oops! There's a sort in front of the map that ought not to be there. And I left out a curly right brace. The code should read:

  my @sorted_data = map {
    "$_->{ field_1 }: $_->{ field_2 }" .
    ( $_->{ field_3 } ? " ($_->{ field_3})" : '' )

  } sort {
    $a->{ field_2 } <=> $b->{ field_2 } ||
    $a->{ field_3 } <=> $b->{ field_3 }

  } @list_of_hashes;

- m.

Author Comment

ID: 7996516
thx mikezone, works perfectly now :)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question