in reply to What does !$saw{$_}++ means

With no change to the idiom, I'd write that as,

my %saw; my @out = grep { ! $saw{$_}++ } @in;
%saw is a hash, initialized empty in the my declaration. I changed that to keep from eliminating and then automatically reproducing (autovivifying) a global %saw.

The expression $saw{$_}++ adds one to the value associated with the key that is $_'s value. It returns the value $saw{$_} had before the addition. $_ is a variable grep sets in turn to each element of @in. "!" is logical "not", so the boolean value $saw{$_} had is inverted. That means grep only sees true when the hash hadn't seen the key yet. Thus, grep only passes along the first instance it sees of each element in @in.

This is an idiom. It seems complicated at first, but will soon be second nature to you. Sometimes it is written to increment $saw{$_} in a loop over @in, and then set @out to keys(%saw), but that doesn't preserve the order of the elements. Yours does.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: What does !$saw{$_}++ means
by Anonymous Monk on Jan 26, 2005 at 09:56 UTC
    With no change to the idiom, I'd write that as,
    my %saw; my @out = grep { ! $saw{$_}++ } @in;
    But that extends the lifetime of %saw. If later in the same (or an inner) block, you need to use the same construct, you have to use a different name for the hash, or do a %saw = () - which will then cause errors if you remove the first construct (until you my the newer construct).

    I like to write it as:

    my @out = do {my %saw; grep !$saw{$_}++, @in};
    which doesn't leak the name of the temporary array.
Re^2: What does !$saw{$_}++ means
by blazar (Canon) on Jan 26, 2005 at 09:06 UTC
    With no change to the idiom, I'd write that as,
    my %saw; my @out = grep { ! $saw{$_}++ } @in;
    Personally I prefer to use the "EXPR-form" of grep() if possible. But this largely depends on the case under examination: in some cases while it could be possible to use that, still it is more terse to adopt the "BLOCK-form", as you did.

    Definitely good point about my %saw; instead. But after all the snippet posted by the OP is too small to really understand wether he's using non-strict code or if he's reusing a previously used %saw (I wouldn't do that, FWIW) or...