On Tue, 2004-05-18 at 18:16, Juerd wrote: > St?phane Payrard skribis 2004-05-18 23:14 (+0200): > > I use over and over this idiom in perl5: > > $a{$_}++ for @a; > > This is nice and perlish but it gets easily pretty boring > > when dealing with many list/arrays and counting hashes.
I never saw the original, but in Perl 5, you would probably just define a class which stored the information you needed. In Perl 6, it should be much easier to make that class behave the way you want syntactically, but for example in Perl 5 (wrap with tie to taste): package CountedArray; sub new { $obj=bless {}, $_[0]; $obj->push(@_) if @_; $obj } sub fetch { my $self = shift; my $index = shift; return $self->{array}[$index]; } sub store { my $self = shift; my $index = shift; my $data = shift; my $old = undef; if ($self->exists($index)) { $old = $self->fetch($index); $self->{counter}{$old}--; } $self->{array}[$index] = $data; $self->{counter}{$data}++; return $old; } sub exists { my $self = shift; my $index = shift; return exists @{$self->{array}}; } sub delete { my $self = shift; my $index = shift; if ($self->exists($index)) { $self->{counts}{$self->{array}[$index]}--; delete $self->{array}[$index]; } } sub count { my $self = shift; my $data = shift; return $self->{counter}{$data}; } sub counts { my $self = shift; return %{$self->{counter}}; } -- Aaron Sherman <[EMAIL PROTECTED]> Senior Systems Engineer and Toolsmith "It's the sound of a satellite saying, 'get me down!'" -Shriekback