I copy and pasted the code sample from above, but it failed to compile. I tried a few things but couldn't get it to work (the error is in the dense part where I don't fully understand the syntactical rules). Offending line seems to be:

snippet:

my %new2 = hmap { $_[1]{nick} =~ /rbush|fred/ ? $_[0] => { hmap { $_[0], $_[1] } %{$_[1]} } : () } %$h;

Here's the compilation error:

bash-3.2$ perl hmap_good2.pl syntax error at hmap_good2.pl line 39, near "] =>" syntax error at hmap_good2.pl line 40, near ":" Execution of hmap_good2.pl aborted due to compilation errors.

Possibly I'm missing something simple? Full script pasted below:

use Data::Dumper; my $h = { 1 => { nick => 'rbush', phone => '5551212', bday => '3/12/1965', }, 3 => { nick => 'ernest', phone => '5553300', bday => '3/12/1971', }, 5 => { nick => 'fred', phone => '1112300', bday => '5/01/1972', }, }; sub hmap (&$) { my $code = shift; my @i = %{ shift() }; my @rv; push @rv, $code->(shift @i,shift @i) while @i; return { @rv }; } my %new2 = hmap { $_[1]{nick} =~ /rbush|fred/ ? $_[0] => { hmap { $_[0], $_[1] } %{$_[1]} } : () } %$h; print Dumper ($new2), "\n";

Note that the second version (the hash ref one) has the same compile error, but I could get that to work by surrounding the returned hash in "()", i.e.:

my $new2 = hmap { $_[1]{nick} =~ /rbush|fred/ ? ($_[0] => hmap { $_[0], $_[1] } $_[1]) : () } $h;

In reply to Re^2: hmap revisited by zerohero
in thread hmap revisited by zerohero

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.