in reply to Re: Simple Hash problem
in thread Simple Hash problem

It is simple, but it doesn't quite match the sample results. This approach ignores elements in %hash that are not in @mips, whereas the original example removed them.

Here's a small rework that fixes it:
my %newhash = (); for (@mips) { $newhash{$_} = $hash{$_} || 999; } %hash = %newhash;

lestrrat's solution is much more fun to look at, though.

Vavoom

Replies are listed 'Best First'.
Re: Re: Re: Simple Hash problem
by trs80 (Priest) on Feb 08, 2002 at 21:38 UTC
    I am not following why you needed to create the temp hash. My output appears to be correct for the example given with the following line of code.

    for(@mips){$hash{$_}||=999}

    UPDATE:
    Thanks crazyinsomniac, after reading these posts several times and talking with you in CB I realize that the example above did not have G in it once it was processed. I read the question and did not examine the output. In the text of the original question it does not state that missing keys should be removed, it merely states that the values should be 999 if they have none.