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

Hi I am kind of stumped here so any help would be appreciated. Basically I have a hash of arrays. What I want to do is take each key and compare it with all the keys in my HoA (other than the current key itself) as well as with every element in the array for every key (and if any element in any of the arrays is the same as the key i am currently comparing with i dont want to compare that either). how would i go about something like this?? thanks.

Replies are listed 'Best First'.
Re: manipulating a hash of arrays
by Zaxo (Archbishop) on Oct 16, 2003 at 03:41 UTC

    You're none too clear about what you're trying to accomplish. The keys of a hash are all unique. You can check existence of keys from an array with grep over a slice,

    my %found_keys; for my $key (keys %hoa) { $found_keys{$key} = [grep { exists $hoa{$_} and $_ ne $key } @{$hoa{$key}}]; }
    That gives you another HoA containing only the elements of the original which match other internal keys than their own.

    I'm not positive that's what you want, but I hope it helps.

    After Compline,
    Zaxo

Re: manipulating a hash of arrays
by jepri (Parson) on Oct 16, 2003 at 03:41 UTC
    You tend to do it a bit like this (untested code, etc):
    foreach my $curr_key ( keys %my_hash) { foreach my $test_key ( keys %my_hash) { if ( $curr_key eq $test_key ) { do_something();} else foreach my $elem ( @{$my_hash{$test_key}}){ if ( $elem eq $curr_key ) { do_something();}

    Update: You could also use the CPAN module Quantum::Superposition to do your searches through arrays. It lets you express your queries in more math-set-theory ways.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: manipulating a hash of arrays
by davido (Cardinal) on Oct 16, 2003 at 03:49 UTC
    What I want to do is take each key and compare it with all the keys in my HoA...

    ...which is probably pointless since hash keys are guaranteed to be unique.

    ...as well as with every element in the array for each key...

    Ok, that's not too difficult.

    (and if any element in any of the arrays is the same as the key i am currently comparing with I don't want to compare that either).

    Huh? I was almost following you until you got to that last part.

    Slow down, think it through, and post an example along with a coherent question. ...and before you hit "submit" be sure that you read it through just once more to make sure that the question actually says what you think it does.


    Dave


    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein
      ...which is probably pointless since hash keys are guaranteed to be unique.

      Which doesn't stop him comparing them. He might be interested to see if one is a substring of the other, or maybe all the keys are numbers, and he wants to compare them to see which ones are bigger or smaller...

      Huh? I was almost following you until you got to that last part.

      I'm guessing a bit here, but I think he doesn't want to compare the keys if one key already "contains" the other key. Still fairly easy to do though.

      ____________________
      Jeremy
      I didn't believe in evil until I dated it.

        I don't know what his intentions are, and would rather not jump to conclusions; it's an exercise that leads to wasted effort.

        I'd rather just see the question re-asked in a way that doesn't require that I guess at its meaning, only to find out later that the time taken in considering and answering the question was wasted on an errant assumption.


        Dave


        "If I had my life to do over again, I'd be a plumber." -- Albert Einstein
Re: manipulating a hash of arrays
by Anonymous Monk on Oct 16, 2003 at 06:53 UTC
    hey thanks guys...i figured it out...just to clarify....yes i know hash keys are unique. as jeremy said the reason i wanted to compare each key against all the keys was to check if the current key was a prefix of the others. if it wasnt, i wanted i wanted to chop off the last letter of the string. Then keep repeating this process for said key till it was its shortest possible form. Basically rpeat this method for all the keys. The idea was to get each key into its shortest unique form. hope that clears things up a bit.