Date: 26 Jun 2001 17:22:09 GMT From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) Subject: Re: how to sort array of similar hashes by one of the hash keys? Message-Id: <9hagc1$t7q$6@mamenchi.zrz.TU-Berlin.DE> According to Ren Maddox : > You've already gotten the solution to your sort question, but I wanted > to make one comment about your initialization code. > > On Tue, 26 Jun 2001, irfan@abstractedge.com wrote: > > > I've split data from a flatfile into hashes in an array, @game, all > > of which have keys 'name', 'score' and 'date'. > > > > @game = (); > > for ($i=0; $i<=$#scores; $i++){ > > ($game[$i]{'name'},$game[$i]{'score'},$game[$i]{'date'}) = split(/,/, > $data[$i]); > > } > > This works fine, but it isn't very Perl-ish (not that there's anything > wrong with that). A more Perl idiomatic way to do this might be: > > foreach (@data) { # or @scores ?? > my %hash; > @hash{qw/name score date/} = split /,/; > push @game, \%hash; > } > > or, if you want to be perverse.... > > @{$game[@game]}{qw/name score date/} = split /,/ for @scores; You win. I had "scalar" before "@game", unnecessarily. I resemble remarks about "perverse"... :) Anno