Sorting Numberic keys in a hash array - with varying number lengths.

Discussion in 'Perl Misc' started by anthony, Dec 9, 2005.

  1. anthony

    anthony Guest

    I had some code that would sort my keys in ascending order - no
    problem.
    Now what I have found is that when I introduce new values which are 6
    digits (rather than 5 digits), it will place these are the beginning of
    the list because they begin with the number 1.
    My code is:
    foreach $PackageToDeploy (sort(keys(%AparsToApply)))
    {
    print
    "\t${PackageToDeploy}\t-\>\t$AparsToApply{$PackageToDeploy}\n";
    }
    which will produce the following:

    57414 -> server1
    57441 -> server1
    57451 -> server1
    57483 -> server1
    57497 -> server1
    57508 -> server1
    57509 -> server1

    Now when a new value is added to the array - lets say 101800. It will
    put this at the top of the list rather than the bottom. It sorts on the
    first digit first, but how can I get it to sort on the weight of the
    number?
    So what I want my output to be is:
    57414 -> server1
    57441 -> server1
    57451 -> server1
    57483 -> server1
    57497 -> server1
    57508 -> server1
    57509 -> server1
    101800 -> server1

    I have had a look at the '$hash{$a} cmp $hash{b}' logic but it doesn't
    seem to sort it the way I want, in fact it doesn't sort it at all.
     
    anthony, Dec 9, 2005
    #1
    1. Advertising

  2. anthony

    Chris Guest

    anthony wrote:
    > I had some code that would sort my keys in ascending order - no
    > problem.
    > Now what I have found is that when I introduce new values which are 6
    > digits (rather than 5 digits), it will place these are the beginning of
    > the list because they begin with the number 1.
    > My code is:
    > foreach $PackageToDeploy (sort(keys(%AparsToApply)))
    > {
    > print
    > "\t${PackageToDeploy}\t-\>\t$AparsToApply{$PackageToDeploy}\n";
    > }
    > which will produce the following:
    >
    > 57414 -> server1
    > 57441 -> server1
    > 57451 -> server1
    > 57483 -> server1
    > 57497 -> server1
    > 57508 -> server1
    > 57509 -> server1
    >
    > Now when a new value is added to the array - lets say 101800. It will
    > put this at the top of the list rather than the bottom. It sorts on the
    > first digit first, but how can I get it to sort on the weight of the
    > number?
    > So what I want my output to be is:
    > 57414 -> server1
    > 57441 -> server1
    > 57451 -> server1
    > 57483 -> server1
    > 57497 -> server1
    > 57508 -> server1
    > 57509 -> server1
    > 101800 -> server1
    >
    > I have had a look at the '$hash{$a} cmp $hash{b}' logic but it doesn't
    > seem to sort it the way I want, in fact it doesn't sort it at all.
    >

    This sorts by value where you want to sort by key. This
    should do the trick:

    foreach $PackageToDeploy (sort {$a <=> $b} keys
    (%AparsToApply)) {
    ...
    }

    HTH
    Chris.
     
    Chris, Dec 9, 2005
    #2
    1. Advertising

  3. anthony

    Anno Siegel Guest

    anthony <> wrote in comp.lang.perl.misc:
    > I had some code that would sort my keys in ascending order - no
    > problem.
    > Now what I have found is that when I introduce new values which are 6
    > digits (rather than 5 digits), it will place these are the beginning of
    > the list because they begin with the number 1.
    > My code is:
    > foreach $PackageToDeploy (sort(keys(%AparsToApply)))
    > {
    > print
    > "\t${PackageToDeploy}\t-\>\t$AparsToApply{$PackageToDeploy}\n";
    > }
    > which will produce the following:
    >
    > 57414 -> server1
    > 57441 -> server1
    > 57451 -> server1
    > 57483 -> server1
    > 57497 -> server1
    > 57508 -> server1
    > 57509 -> server1
    >
    > Now when a new value is added to the array - lets say 101800. It will
    > put this at the top of the list rather than the bottom. It sorts on the
    > first digit first, but how can I get it to sort on the weight of the
    > number?


    You're using the default sort which sorts alphabetically. See perldoc
    -f sort for alternatives.

    > So what I want my output to be is:
    > 57414 -> server1
    > 57441 -> server1
    > 57451 -> server1
    > 57483 -> server1
    > 57497 -> server1
    > 57508 -> server1
    > 57509 -> server1
    > 101800 -> server1
    >
    > I have had a look at the '$hash{$a} cmp $hash{b}' logic but it doesn't
    > seem to sort it the way I want, in fact it doesn't sort it at all.


    That would sort by the values which are all "server1". What made
    you think that might help?

    The sort documentation has examples for numeric sorting. Apply them.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Dec 9, 2005
    #3
  4. anthony

    MikeGee Guest

    anthony wrote:
    > I had some code that would sort my keys in ascending order - no
    > problem.
    > Now what I have found is that when I introduce new values which are 6
    > digits (rather than 5 digits), it will place these are the beginning of
    > the list because they begin with the number 1.
    > My code is:
    > foreach $PackageToDeploy (sort(keys(%AparsToApply)))


    Just change this to:
    foreach $PackageToDeploy (sort {$a <=> $b} (keys(%AparsToApply)))

    sort uses {$a cmp $b} by default; which sorts ASCIIbetically.


    > {
    > print
    > "\t${PackageToDeploy}\t-\>\t$AparsToApply{$PackageToDeploy}\n";
    > }
    > which will produce the following:
    >
    > 57414 -> server1
    > 57441 -> server1
    > 57451 -> server1
    > 57483 -> server1
    > 57497 -> server1
    > 57508 -> server1
    > 57509 -> server1
    >
    > Now when a new value is added to the array - lets say 101800. It will
    > put this at the top of the list rather than the bottom. It sorts on the
    > first digit first, but how can I get it to sort on the weight of the
    > number?
    > So what I want my output to be is:
    > 57414 -> server1
    > 57441 -> server1
    > 57451 -> server1
    > 57483 -> server1
    > 57497 -> server1
    > 57508 -> server1
    > 57509 -> server1
    > 101800 -> server1
    >
    > I have had a look at the '$hash{$a} cmp $hash{b}' logic but it doesn't
    > seem to sort it the way I want, in fact it doesn't sort it at all.
     
    MikeGee, Dec 9, 2005
    #4
  5. anthony wrote:
    > I had some code that would sort my keys in ascending order - no
    > problem.
    > Now what I have found is that when I introduce new values which are 6
    > digits (rather than 5 digits), it will place these are the beginning
    > of the list because they begin with the number 1.
    > My code is:
    > foreach $PackageToDeploy (sort(keys(%AparsToApply)))

    [...]
    > Now when a new value is added to the array - lets say 101800. It will
    > put this at the top of the list rather than the bottom. It sorts on
    > the first digit first,


    Unless told otherwise

    > but how can I get it to sort on the weight of the number?


    Well, unless told otherwise sort() will sort in lexicographical order.

    > I have had a look at the '$hash{$a} cmp $hash{b}' logic


    Right idea, wrong operator. Now you are asking explicitely(!) for a
    lexicographical sort.
    If you want to sort numerically then use the numerical comparison operator
    <=> instead.

    jue
     
    Jürgen Exner, Dec 9, 2005
    #5
  6. MikeGee wrote:
    > sort uses {$a cmp $b} by default; which sorts ASCIIbetically.


    sort() is better than that. It even sorts alphabetically, not only just
    ASCIIbetically.

    jue
     
    Jürgen Exner, Dec 9, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grumfish
    Replies:
    2
    Views:
    377
    Bengt Richter
    Aug 19, 2003
  2. jt
    Replies:
    3
    Views:
    936
    Keith Thompson
    May 23, 2005
  3. close browser without Logout

    Numberic textbox

    close browser without Logout, Aug 21, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    125
    close browser without Logout
    Aug 21, 2006
  4. jmhmaine

    Inserting Text before the Numberic Page numbers in paging

    jmhmaine, Feb 28, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    162
    Ken Cox [Microsoft MVP]
    Feb 28, 2005
  5. rp
    Replies:
    1
    Views:
    556
    red floyd
    Nov 10, 2011
Loading...

Share This Page