Your post led me to believe your user ID's would be numeric. I don't think I quite understand what all of the "1, 2, ... , n | n != 1 or 2" is supposed to mean. ID's incrementing but starting at 3? If your intent is to associate a username and an ID, by all means a hash is what you want. See the documentation for sort, and perhaps How do I sort a hash (optionally by value instead of key)?. To find the largest ID, you can use 'max' from List::Util on the values of the %hash, but you're better off storing that in a variable unless your script only needs to use this once.
Regardless, this is pretty basic hash usage stuff. You would benefit more from a book like Learning Perl or the Perl Cookbook. | [reply] [d/l] |
ok :)
First off, thanks a lot for the help. As I've mentioned, I'm
still really new to this stuff, and places like this make things
a lot easier.
Now, to the dirty stuff. Yes, the sorting of the hash is no
problem. I do own a book called "mastering PERL" (just went
out and bought it yesterday), but I can't seem to find any help
on setting the hash for this case.
All I want to know is how to set the hash properly, having
two arrays (one holding the UIDs and one holding the "points
values) of an undefined length, so that the UIDs and the
"points" values will be put into the hash.
After that's set, I have the sort under control. I just
need to know how to properly set that hash.
For arrays, there's push; is there anything like this for
hashes?
| [reply] |
Use a hash slice: @hash{@uids} = @points;
my @uids = (0..9);
my @points = qw/1 3 2 5 10 3 2 1 3 0/;
my %hash;
@hash{@uids} = @points;
foreach (sort { $hash{$b} <=> $hash{$a} } keys %hash) {
print "$_: $hash{$_}\n";
}
Tony | [reply] [d/l] [select] |