in reply to Re: promoting array to a hash
in thread promoting array to a hash

Yeah, there's nothing wrong with my code above, its just that I was wondering how to get rid of the unnecessary temporary variable %words. For example the following snippet...
@a = keys (a=>1,b=>2,c=>3);
...produces the following error...
Type of arg 1 to keys must be hash (not list), blah, blah, blah
...but I'm willing to bet that there is some syntax to fix the problem.
#This doesn't work @a = keys %{(a=>1,b=>2,c=>3)};

Replies are listed 'Best First'.
Re^3: promoting array to a hash
by Zaxo (Archbishop) on Jun 13, 2004 at 05:41 UTC

    Oh, Ok, you almost have it, @a = sort keys %{{a=>1,b=>2,c=>3}}; or in terms of your original problem, @a = sort keys %{{ map {$_ => undef} map {split} <> }}; Notice the replacement of parens with curlies. That makes the hashlike list into a hash reference to its contents, and the outer %{} dereferences it.

    I agree with your desire to avoid temporary variables, I try to do that, too, in perl.

    After Compline,
    Zaxo

      thanks, that's just what I was looking for.
      print "$_\n" for sort keys %{{map {$_,1} grep /^[a-z]+$/, (split /\s/, + join(" ",<>))}};