in reply to extract number of times a value appears in an array

#!/usr/bin/perl -w use strict; use Data::Dumper; my $count={}; my @a =qw(1 2 3 4 5 5 5 5); map { $count->{$_}++ } @a; print Dumper($count);

grep and map in perl

Replies are listed 'Best First'.
Re^2: extract number of times a value appears in an array
by davorg (Chancellor) on Aug 18, 2006 at 14:39 UTC

    Whilst recent versions of Perl do away with the memory problems of using map in a void context, it still seems strange to me to use map purely for its side effects. Particularly when for usually works just as well.

    $count->{$_}++ for @a;
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg