Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am building an affinity algorithm with Perl. After all the calculation, I will get a list of number for example;-

$string1 = 323

$string2 = 122

$string3 = 233

Then I want the output to be display in a descending order. So how do I wrote the program, as I wouldn't know the result of this calculation.

Replies are listed 'Best First'.
Re: sorting string
by Zaxo (Archbishop) on Aug 28, 2005 at 07:14 UTC

    If you'll put your results in an array instead of a bunch of scalars, you can avoid dealing with symbolic references in that. That also makes the sort easy.

    @strings = sort { $b <=> $a } @strings;

    That may not be useful. You'd probably want some record of the original string, and that's probably what you really want to sort. Put the numbers into a hash with the strings as keys.

    my %code = map { $_ => calculation($_) } @strings; @strings = sort { $code{$b} <=> $code{$a} } keys %code;

    After Compline,
    Zaxo

Re: sorting string
by davido (Cardinal) on Aug 28, 2005 at 07:11 UTC

    Try this:

    print "$_\n" for sort { $b <=> $a } ( $string1, $string2, $string3 )

    Note, this illustrates the contortions you put yourself through by using variables such as $string1, $string2, and $string3 when you really ought to be using a proper array such as @strings. Look at how the snippet improves if you use a proper array:

    print "$_\n" for sort { $b <=> $a } @array;

    It could also be written as:

    print map { "$_\n" } sort { $b <=> @a } @array;

    Or even...

    { local $, = "\n"; print( sort( { $b <=> $a } @array ), '' ); }

    By the way; the title to your question, "Sorting strings" seems to be a misnomer, since in your question you seem to be asking how to sort numeric values. Maybe I've missed the point. But if you're trying to sort ascii-betically (instead of numerically), use the cmp operator instead of <=>. This and more is discussed in sort.


    Dave

      But how do I know which belong to which string? It will list something like this '123343322' so how do I save the list in a database for example? if it could list string1 = 122, string = 222, string 3 = 333... TQ

        Yes, after posting my response it hit me that your question simply didn't ask what you wanted answered.

        Zaxo has it right; you probably want a hash with key/value relationship. Then you ultimately want the strings stored as keys, and the computed values as values associated with the keys. ...something like this:

        use strict; use warnings; my %strings; # Set up some example key/value relationships. Your method # for populating the hash will be more complex, but isn't # defined in the question. $strings{"Here is one string"} = 233; $strings{"Here is another string"} = 151; $strings{"Here is yet another"} = 322; print map{ "$_ = $strings{$_}\n" } sort{ $strings{$b} <=> $strings{$a} } keys %strings;

        Dave

Re: sorting string
by GrandFather (Saint) on Aug 28, 2005 at 07:09 UTC

    Generally the monks like to see a little effort before they are inclined to give a lot of help. However, in this case, and only because I'm kind, I will point you to here where you will find documentation for the relational operators and here where you will find documentation on sort.

    One decision you need to make is wether you wish to perform a numeric sort of a string sort. You will find the <=> (space ship) is good for numeric sorting and that cmp is good for string sorting.


    Perl is Huffman encoded by design.
Re: sorting string
by gargle (Chaplain) on Aug 28, 2005 at 07:05 UTC
    perl -e '@list = (323,122,233); print sort @list;'
      The poster showed almost no effort, but this shows even less. They asked for descending order of a series of numbers, and you gave them ascending order as strings, printed without spacing of any kind.

      --
      [ e d @ h a l l e y . c c ]