From kevinc at gci.net Tue Sep 28 09:27:21 1999 From: kevinc at gci.net (Kevin J Creason) Date: Wed Aug 4 23:56:56 2004 Subject: Sorting Arrays Message-ID: <37F0D049.2FC82522@gci.net> Here's what I'm doing, and I'm sure I am doing it backwards to some degree so please set me straight. I've got a directory where I create empty files that are the candidates' names in an online election. As each district places their total of votes they are appended to the candidates file with district name and votes. So my "top" array is the canidates names, another toplevel array is created that is the total votes for that candidates: @canlist @tvote But each candidate file is opened and that information (district # and votes) is dumped into two other arrays: @district{$c,$vc} and $subvote{$c,$vc} The scalar 'c' matches the current canlist/tvote and 'vc' increments for the each district/vote entry. Subvotes are added together to create one of the array entries $tvote. I hold on to the district for later statistics by district. Here is my dilemma: I want to sort my @canlist and tvote by tvote, so that highest vote getters are listed on top instead of alphabetically. I'm sure there is a way, I just haven't found it yet, but I also have to keep the other arrays in sync. Any ideas? If I have to completely recode that _may_ not be a bad thing... :) TIA Kevin ================================================= Mailing list info: If at any time you wish to (un|re)subscribe to the list send the request to majordomo@hfb.pm.org. All requests should be in the body, and look like such subscribe anchorage-pm-list unsubscribe anchorage-pm-list From corliss at odinicfoundation.org Tue Sep 28 11:43:13 1999 From: corliss at odinicfoundation.org (Arthur Corliss) Date: Wed Aug 4 23:56:57 2004 Subject: Sorting Arrays In-Reply-To: <37F0D049.2FC82522@gci.net> Message-ID: On Tue, 28 Sep 1999, Kevin J Creason wrote: > Here's what I'm doing, and I'm sure I am doing it backwards to > some degree so please set me straight. I've got a directory > where I create empty files that are the candidates' names in > an online election. As each district places their total of > votes they are appended to the candidates file with district > name and votes. > So my "top" array is the canidates names, another toplevel > array is created that is the total votes for that candidates: > @canlist @tvote > > But each candidate file is opened and that information > (district # and votes) is dumped into two other arrays: > @district{$c,$vc} and $subvote{$c,$vc} > The scalar 'c' matches the current canlist/tvote and 'vc' > increments for the each district/vote entry. > > Subvotes are added together to create one of the array entries > $tvote. I hold on to the district for later statistics by > district. > > Here is my dilemma: I want to sort my @canlist and tvote by > tvote, so that highest vote getters are listed on top instead > of alphabetically. I'm sure there is a way, I just haven't > found it yet, but I also have to keep the other arrays in > sync. > Any ideas? If I have to completely recode that _may_ not be a > bad thing... :) > TIA > Kevin One way to do that (albeit by brute force) is a simple bubble sort. Instead of two arrays, though, I might recommend a hash of candidate => votes, and an array whose indexed order will be the sorted order. Perhaps: @by_votes = (keys %candidate); for ($m = 0; $m < scalar @by_votes; $m++) { for ($i = $m + 1; $i < scalar @by_votes; $i++) { @by_votes[$m,$i] = @by_votes[$i,$m] if ($candidate{$by_votes[$m]} < $candidate{$by_votes[$i]}); } } --Arthur Corliss Bolverk's Lair -- http://www.odinicfoundation.org/arthur/ "Live Free or Die, the Only Way to Live" -- NH State Motto ================================================= Mailing list info: If at any time you wish to (un|re)subscribe to the list send the request to majordomo@hfb.pm.org. All requests should be in the body, and look like such subscribe anchorage-pm-list unsubscribe anchorage-pm-list From michael at shoebox.net Tue Sep 28 17:28:43 1999 From: michael at shoebox.net (Michael Fowler) Date: Wed Aug 4 23:56:57 2004 Subject: Sorting Arrays In-Reply-To: <37F0D049.2FC82522@gci.net>; from Kevin J Creason on Tue, Sep 28, 1999 at 06:27:21AM -0800 References: <37F0D049.2FC82522@gci.net> Message-ID: <19990928142843.A301@shoebox.net> I'm going to attempt an answer, but I was somewhat confused by the terminology and syntax used at points. On Tue, Sep 28, 1999 at 06:27:21AM -0800, Kevin J Creason wrote: > @canlist @tvote These are what I assume you're trying to sort for. > @district{$c,$vc} and $subvote{$c,$vc} This implies that %district is a hash, and you're pulling multiple keys out at once. From the syntax, %subvote is a hash, and you're looking up the key "$c$;$vc", $; being the subscript seperator (perldoc perlvar). > Here is my dilemma: I want to sort my @canlist and tvote by > tvote, so that highest vote getters are listed on top instead > of alphabetically. I'm sure there is a way, I just haven't > found it yet, but I also have to keep the other arrays in > sync. >From this I'm going to assume you have two arrays, @canlist and @tvote. Each element in @canlist has a corresponding element in @tvote, at the same array index. Here is my suggestion for sorting this: @index = sort { $tvote[$b] <=> $tvote[$a] } (0 .. $#tvote); Now @index contains the array indexes in @canlist and @tvote that correspond to the highest vote-getter to the least. E.g. $canlist[$index[0]] will give you the candidate with the highest number of votes, $canlist[$index[-1]] will give you the candidate with the least number of votes. A solution that would probably be easier to deal with is to integrate your lists together into a hash: %candidates = map { $canlist[$_], $tvote[$_] } (0 .. $#canlist); Now, sort the candidates by votes: @keys = sort { $candidates{$b} <=> $candidates{$a} } keys(%candidates); Now @keys contains the candidates' names from highest vote-getter to least. E.g. $keys[0] is the name of your highest vote-getter, $keys[-1] is the name of the lowest vote-getter. The number of votes for a given candidate can be retrieved by lookup up the candidate's name in the hash, $candidate{$keys[0]} for your highest vote-getter's votes. Hopefully that helps a bit. Michael -- There isn't a mome rath alive that can outgrabe me. -- ================================================= Mailing list info: If at any time you wish to (un|re)subscribe to the list send the request to majordomo@hfb.pm.org. All requests should be in the body, and look like such subscribe anchorage-pm-list unsubscribe anchorage-pm-list From kevinc at gci.net Wed Sep 29 00:47:37 1999 From: kevinc at gci.net (Kevin Creason) Date: Wed Aug 4 23:56:57 2004 Subject: Sorting Arrays Message-ID: <001601bf0a3e$236a91a0$0b00030a@kevinj.creasonfam.net> Thank you (Michael, Arthur, and Leif for thinking about it anyway) for the answers... I will start monkeying tomorrow and try to get back to the group with some feedback or clarification. -----Original Message----- From: Michael Fowler To: Kevin J Creason Cc: Anchorage Perl Mongers Date: Tuesday, September 28, 1999 2:28 PM Subject: Re: Sorting Arrays >I'm going to attempt an answer, but I was somewhat confused by the >terminology and syntax used at points. > > >On Tue, Sep 28, 1999 at 06:27:21AM -0800, Kevin J Creason wrote: >> @canlist @tvote > >These are what I assume you're trying to sort for. > > >> @district{$c,$vc} and $subvote{$c,$vc} > >This implies that %district is a hash, and you're pulling multiple keys out >at once. From the syntax, %subvote is a hash, and you're looking up the key >"$c$;$vc", $; being the subscript seperator (perldoc perlvar). > > >> Here is my dilemma: I want to sort my @canlist and tvote by >> tvote, so that highest vote getters are listed on top instead >> of alphabetically. I'm sure there is a way, I just haven't >> found it yet, but I also have to keep the other arrays in >> sync. > >From this I'm going to assume you have two arrays, @canlist and @tvote. >Each element in @canlist has a corresponding element in @tvote, at the same >array index. Here is my suggestion for sorting this: > > @index = sort { $tvote[$b] <=> $tvote[$a] } (0 .. $#tvote); > >Now @index contains the array indexes in @canlist and @tvote that correspond >to the highest vote-getter to the least. E.g. $canlist[$index[0]] will give >you the candidate with the highest number of votes, $canlist[$index[-1]] >will give you the candidate with the least number of votes. > >A solution that would probably be easier to deal with is to integrate your >lists together into a hash: > > %candidates = map { $canlist[$_], $tvote[$_] } (0 .. $#canlist); > >Now, sort the candidates by votes: > > @keys = sort { $candidates{$b} <=> $candidates{$a} } keys(%candidates); > >Now @keys contains the candidates' names from highest vote-getter to least. >E.g. $keys[0] is the name of your highest vote-getter, $keys[-1] is the name >of the lowest vote-getter. The number of votes for a given candidate can be >retrieved by lookup up the candidate's name in the hash, >$candidate{$keys[0]} for your highest vote-getter's votes. > > >Hopefully that helps a bit. > > >Michael >-- >There isn't a mome rath alive that can outgrabe me. >-- ================================================= Mailing list info: If at any time you wish to (un|re)subscribe to the list send the request to majordomo@hfb.pm.org. All requests should be in the body, and look like such subscribe anchorage-pm-list unsubscribe anchorage-pm-list From corliss at odinicfoundation.org Wed Sep 29 11:41:43 1999 From: corliss at odinicfoundation.org (Arthur Corliss) Date: Wed Aug 4 23:56:57 2004 Subject: Sorting Arrays In-Reply-To: <001601bf0a3e$236a91a0$0b00030a@kevinj.creasonfam.net> Message-ID: On Tue, 28 Sep 1999, Kevin Creason wrote: > Thank you (Michael, Arthur, and Leif for thinking about it anyway) for the > answers... I will start monkeying tomorrow and try to get back to the group > with some feedback or clarification. Looks like you've got several workable ways, but after reading Michael's, I have to say that sorting on hash values seems to be the most effective, and something I completely overlooked myself. I'm so used to having to whip up sorting algorithms myself, but the method that Perl uses for sort is *much* faster than a generic bubble sort. . . --Arthur Corliss Bolverk's Lair -- http://www.odinicfoundation.org/arthur/ "Live Free or Die, the Only Way to Live" -- NH State Motto ================================================= Mailing list info: If at any time you wish to (un|re)subscribe to the list send the request to majordomo@hfb.pm.org. All requests should be in the body, and look like such subscribe anchorage-pm-list unsubscribe anchorage-pm-list