• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

Sorting problem....

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.
Maldini
0
Maldini
Asked:
Maldini
  • 2
  • 2
1 Solution
 
mikezoneCommented:

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 ) {
    /^([^:]+):\s(\d+)(?:\s\((\d+)\))?$/;
    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.

    /^([^:]+):\s

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

   (\d+)

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

   (?:\s\((\d+)\))?$/;

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.
0
 
MaldiniAuthor Commented:
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. :)
0
 
mikezoneCommented:
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.
0
 
MaldiniAuthor Commented:
thx mikezone, works perfectly now :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now