Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!decwrl!mcnc!uvaarpa!mmdf From: bjaspan@athena.mit.edu (Barr3y Jaspan) Newsgroups: comp.lang.perl Subject: default associative array sort function Message-ID: <1991May14.045945.11077@uvaarpa.Virginia.EDU> Date: 14 May 91 04:59:45 GMT Sender: mmdf@uvaarpa.Virginia.EDU (Uvaarpa Mail System) Reply-To: bjaspan@athena.mit.edu Organization: The Internet Lines: 27 I frequently have associative arrays that map strings to integers; say, $users{$username} = # of pages printed on a printer. Sorting such an associative array requires writing a special-purpose function sub sort_users { $users{$a} <=> $users{$b}; } and then call sort sort_users %users. This works fine but is a pain; I have to create a separate sort function for each assoc array, when really I shouldn't have to create one at all (this is a frequent enough operation, I think...) How about adding a default sort-assoc-array-{numerically, lexigraphically} function, so that (sort MAGIC_FUNC %users) would work? Even better would be if the sort function recognized that it was dealing with an assoc array, figured out whether the values were numbers or strings, and choose the right value, so that (sort %users) would do the right thing. ("Figuring out whether the values are numbers or strings" is, of course, not necessarily trivial. You could implement some specific heuristic and document it, given the programmer the choice of overriding the heuristic when necessary.) print((sort (' another', ' Just', ' perl ', 'hacker,')), "\n"); Barr3y