mseabrook has asked for the wisdom of the Perl Monks concerning the following question:

Wherein I call on the perl monks to aid me in my misguided quest to reduce some perfectly understandable, and fairly minimal code into a one-liner.

Given the following code, which uses a hash find the unique things in a list of such,

my %this_to_that = map {$_->this => $_} @things; return values %this_to_that;

How does one obviate the need for the temporary (named) hash? I naively expect something like the following to work, but does it does not.

return values %{map {...} @things} Type of arg 1 to values must be hash (not anonymous hash ({})) at...

Is it possible to do this with an anonymous hash?

Replies are listed 'Best First'.
Re: Get values from anonymous hash?
by blue_cowdawg (Monsignor) on Aug 17, 2011 at 19:40 UTC

    After reading your post, I'm still not clear on what you are trying to accomplish. However, consider the following code:

    #!/usr/bin/perl -w use strict; use Data::Dumper; my @foo = qw/ name Roger age 20 hobby lint /; my %thing = @foo; print Dumper(\%thing);

    If you run that code you get something on the order of:

    $VAR1= { 'hobby' => 'lint', 'name' => 'Roger', 'age' => 20 };

    I have no idea otherwise what your trying to accomplish.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Get values from anonymous hash?
by CountZero (Bishop) on Aug 17, 2011 at 19:46 UTC
    Is this what you are looking for?
    use Modern::Perl; my @things = qw /one two three one four four five six two seven/; say join ' - ', keys %{{map {$_, 1} @things}};
    output: three - six - five - one - seven - two - four

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Yep, that works, thanks. I needed to wrap the anonymous hash in another set of curly brackets before trying to de-reference it.
Re: Get values from anonymous hash?
by parv (Parson) on Aug 17, 2011 at 19:25 UTC

    Given the above use of values, just return @things.

    ...unless this modifies $_ in which case you need to make a hash reference (which you had not done yet) out of map result before you dereference via BLOCK usage.

Re: Get values from anonymous hash?
by blue_cowdawg (Monsignor) on Aug 17, 2011 at 19:45 UTC

    By the way... if all you are trying to do is get the values from an anonymous hash can be summed up as:

    my @ry = values %{ { foo => bar , bing => baz , up=>left }};
    Give it a try....


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      That is indeed what I'm looking for, thanks. I was missing a curly bracket. I tried,
      values %{map {...}};
      and
      values %{(map {...})};
      but not
      values %{{map {...}}};