Off the top of my head, a relatively inefficient method:

eval '$Hash' . map { qq({'$_'}) } @a . ' = 1';

Untested, of course ;-) A bit of description in order here: we're just creating the string you wanted. A bit more smarts to only put in the quotes when required, check if the hash you're trying to create already exists as a scalar rather than a hashref, etc., to prevent collisions, ... but if you know that collisions can't happen, then this should be fine.

UPDATE:After reading the other hash thread, I see someone of importance saying "don't use eval". And, as a general rule, I would agree. But I don't know if this is a one-off script (just do a quick conversion, and never use it again), or if the input is tightly controlled (e.g., can't be manipulated to circumvent perl security-isms, e.g., a string that had single-quotes in it: qq(blah' . system("rm -rf /") . 'blah)), or other mitigating circumstances. Sometimes quick and dirty is fine. A more robust method would use loops or even recursion to do this.

sub create_hash_depth { my $href = shift; my @a = @_; if (scalar @a > 1) { $href->{$a[0]} = {}; create_hash_depth($href->{$a[0]}, @a[1..$#a]); } else { $href->{$a[0]} = 1; } }

Or something like that


In reply to Re: Hash key manipulation question by Tanktalus
in thread Hash key manipulation question by EchoAngel

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.