in reply to Re: hash creation.
in thread hash creation.

I guess I didn't define the problem good enough :)
 
Anyways, I really need the user ID to stick with the value during the sort so that I can display the user name and the value on a page with the highest value first, and so on, so I think that I have to use a hash. If this isn't the case, please inform me!
 
Regardless, the problem I'm having is with setting the values for the undetermined amount of users. If I knew or could limit the amount of users, things would be a lot simpler, but the script should be able to do this on its own.

Replies are listed 'Best First'.
Re: Re: Re: hash creation.
by Fastolfe (Vicar) on Jan 03, 2001 at 19:39 UTC
    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.

      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?
        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