Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!hplabs!hpl-opus!hpnmdla!chrise From: chrise@hpnmdla.hp.com (Chris Eich) Newsgroups: comp.lang.perl Subject: Re: Sortin based on part of an associative array? Message-ID: <8310026@hpnmdla.hp.com> Date: 15 Apr 91 23:42:17 GMT References: <17524@venera.isi.edu> Organization: HP Network Measurements Div, Santa Rosa, CA Lines: 31 In comp.lang.perl, tchrist@convex.COM (Tom Christiansen) writes: sub sort_by_field { local(*table, $field) = @_; local($_, @keys); for (keys %table) { push(@keys, (split(/#/, $table{$_}))[$field]); } sort bynum @keys; } @Images{&sort_by_len} is all the elements in length order; @Images{&sort_by_wid} is all the elements in width order. This doesn't seem to work; sort_by_len returns the lengths, not the keys of the original table. You seem to want sort_by_field to return the keys of the original table, so how about this: sub sort_by_field { local(*Table, $Field) = @_; local(%Keys); foreach (keys %Table) { $Keys{(split(/:/, $Table{$_}))[$Field]} = $_; } @Table{@Keys{sort by_num keys %Keys}}; } Chris