in reply to promoting array to a hash

I don't see anything wrong with what you have, but you may be looking for a hash slice. Leaving out the grep selection stuff,

my %words; while (<>) { @words{ split } = (); }
I'm not sure what you mean by sorted here, hashes don't support any stable order.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: promoting array to a hash
by sleepingsquirrel (Chaplain) on Jun 13, 2004 at 05:29 UTC
    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)};

      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(" ",<>))}};