> -----Original Message----- > From: Gary Stainburn [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, May 01, 2002 9:50 AM > To: [EMAIL PROTECTED]; [EMAIL PROTECTED] > Cc: Perl Help > Subject: Re: HELP! > > > On Wednesday 01 May 2002 1:43 pm, fliptop wrote: > > Andrew Rosolino wrote: > > > Say I have an array that contains! > > > > > > @array = qw( cool cool dog dog dog ); > > > > > > Now I want to sum that up, so that the array only has > > > 1 COOL and 1 DOG, so it would contain! > > > > > > @array = qw( cool dog ); > > > > my %array; > > > > map { $array{$_}++ } @array; > > What's the difference between the above, and the below, and > why is one better > than the other? > > {$array{$_}++} foreach @array;
They are the same as far as the effect on %array. But you shouldn't use map() in a void context, as it constructs a result list which is then thrown away. So your iterative approach is better. Better still IMO is: { my %seen; @array = grep { !$seen{$_}++ } @array; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]