in reply to How to compare hash value

I'm not sure what you mean when you say you want to group the arrays together. Are you adding a new hash entry, pushing them on an array somewhere?

Since I don't understand your grouping, I'll just tackle the other part of your question. One way to compare arrays would be to create a "stringify" function:

# note that having a sub for this is overkill, but if you # need more functionality, it's easy to encapsulate here (such # as making it case-insensitive to independant of order) sub stringify { local $" = ""; return "@{$_[0]}" } if (stringify([sort @array1]) eq stringify([sort @array2])) { # arrays are the same }

At that point, to find all keys that have identical values:

my %matches; while (my ($key,$value) = each %somehash) { # stringified arrays as keys push @{$matches{stringify([sort @$value])}} => $key; }

The above code snippet probably doesn't do what you want, but it might give you a starting point. Each stringified array will now point to a list of keys that match it.

Cheers,
Ovid

New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)

Replies are listed 'Best First'.
Re: Re: How to compare hash value
by Jasper (Chaplain) on Apr 23, 2003 at 14:21 UTC
    This (simple and easy) method has a tendency to fall down sometimes. If you had 'abc', 'def' in one array, and 'a', 'b', 'c', 'd', 'e', 'f' in another, joining with the empty string will make the strings equal. The simple solution would be to make $" something bizarre, and not likely to appear in the array, like "bcd". Only joking! Maybe ":::", or something.

    If you can't be sure of what's in the arrays, comparison of Data::Dumper output of the sorted array might work?

    Jasper

      Whoops! I hadn't thought of that. Nice catch.

      Cheers,
      Ovid

      New address of my CGI Course.
      Silence is Evil (feel free to copy and distribute widely - note copyright text)

      To get over that you must encapsulate Sigma, so that a condition like that does not arise. If you convert to a smaller subset and then use out of band data that will be safe (example - convert to hex and join with spaces). Also safe is escaping - because that's basically just creating a wide charset, which is a larger Sigma, enabling data out of the UTF or ASCII sigma. This is possibly faster, and more memory efficient.

      If you join("\\",map { quotemeta($_) } @array); you will get a safe value, because quotemeta ensures that a single \ will never appear in the values.

      -nuffin
      zz zZ Z Z #!perl