An alternative usage for what you posted, having the same end-result is

@saw{@in} = undef;

Because of autovivification-related bugs, I got into the habit of using defined instead of exists to determine whether a hash contains a certain key. This habit required developing the second habit of using something like

@saw{@in} = (1) x @in;
instead of
@saw{@in} = ();
As I say, these are just programming habits; with a modicum of vigilance it is perfectly possible to avoid autovivification bugs and still use the @saw{@in} = () idiom. In fact, vigilance would be cheaper than my habit, which costs me a 10% premium in the size of the resulting hashes:
DB<1> use Devel::Size 'total_size' DB<2> @x{1..1000}=() DB<3> @y{1..1000}=(1) x 1000 DB<4> p total_size(\%x) 41049 DB<5> p total_size(\%y) 45049
I'm willing to pay this price to avoid heart-stopping encounters with lines like this in my code
if ( exists $some_hash{ foo } ) { launch_the_missiles(); }
and the resulting frantic code scan to ensure that it doesn't contain anything like
$x = $some_hash{ maybe_foo() }{ bar };

Now I use exists only when dealing with the rare hash for which undef is a legitimate value.

Update: A related idiom, which, in addition to removing duplicates, also preserves the original order as much as possible, is:

my @no_repeats = do { my %h; grep !$h{$_}++, @has_repeats };

the lowliest monk


In reply to Re: need explanation of @foo{@bar} = (); (hash slice) by tlm
in thread need explanation of @foo{@bar} = (); (hash slice) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.