The reason get_ids({'id_one','id_two','id_three'}) doesn't work is because it is taking each pair of values as a key/value pair. So that line is constructing a hash { id_one=>'id_two', id_three=>undef }.

If you were running with warnings enabled, you would be receiving a message Odd number of elements in hash assignment at... which would have given you a clue here

Without asking why you want to do this, though from what you've told us it seems bizaar:), you could do

get_ids( {id_one=>undef, id_two=>undef, id_three=>undef } );

However, if the idea is that you are going to return the hash to the caller with the values for the keys supplied filled in, it might be better to pass a ref to an anon. array of the values and then construct the hash internally like this.

sub get_ids { my %hash; @hash{@{shift()}} = undef; print "key:$_\n" for keys %hash; return \%hash; } # Call it this way my $hashref = get_ids( [qw/id_one id_two id_three/] );

If you run that, you'll get output

key:id_one key:id_three key:id_two

Nah! You're thinking of Simon Templar, originally played (on UKTV) by Roger Moore and later by Ian Ogilvy

In reply to Re: Pass anonymous hash - keys only? by BrowserUk
in thread Pass anonymous hash - keys only? by blahblah

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.