i'm not sure of your requirements. you say you need unique keys... is that because there may be two "keith"s? what if they each get a 90? in your implementation, they're still not unique. or is it because rob and john might both get a 52? if this is a hash value, then there isn't a uniqueness problem.

if the name alone is good for uniqueness, you can use this simple short method, which works with the data you provided.

#!/usr/bin/perl -w use strict; $|++; my @names_marks = ( 'cliff 76', 'john 52', 'keith 90', 'rob 52' ); my %hash = map { split } @names_marks; my @sorted_names = sort { $hash{$b} <=> $hash{$a} } keys %hash; print $hash{$_},' ',$_,$/ for @sorted_names;
also, your code
foreach (@names_marks) { ($names[$_], $marks[$_]) = split / /, $_; $hash{"$marks[$_]".$names[$_]} = ucfirst($names[$_]). " " . $marks[$ +_]; }
is not correct. $_ contains the value from @names_marks, not the index. so you're trying to access $names[keith], which isn't numeric. you cannot ignore these warnings, your code is broken.

anytime you think 'unique', think hash. i don't think you need arrays at all, but you might know something i don't.

~Particle ;Þ


In reply to Re: sort hash elements... by particle
in thread sort hash elements... by kiat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.