On Thu, 28 Jun 2001, twelveoaks <[EMAIL PROTECTED]> wrote,
> Peter Scott Wrote:
>
> > my %h;
> > @h{@vars} = ();
> > if (keys %h != @vars) { $youlose = "yes"; }
>
>
> Maybe I'm missing something - won't these *always* match, since @vars has
> been used to create keys %h?
No, depends on the content of @array.
When you use the array @vars to create the keys, the uniqueness is
guaranteed. Peter gave you the code to prove that (I didn't test
it though). Imagine you have,
@array = qw(one one one two); # 4 elements
Since the keys must be unique, only two of the four elements fill
in the key slots, they are 'one' and 'two'. Let's create the hash
manually,
%h = (
one => 1,
one => 2,
one => 3,
two => 4,
);
You only get two keys here (the last value will override the the
previous, so in this case, $h{one} will be 3). So the comparison,
keys %h != @vars
yields true, they're not equal.
> It seems that way when I test it.
>
> What I want to detect is whether any two of the values within @vars are
> identical.
>
> Will this do that?
There's another approach, still using hash. Initialize an empty hash,
say %seen. Iterate the @vars and check whether the current element
has been seen.
my %seen;
foreach my $elem (@vars) {
if (exists $seen{$elem}) {
$youlose = 'yes';
} else {
$seen{$elem} = ''; # empty string will be sufficient,
# but see below
}
}
But it's convenient to do the check and keep in single step,
if ($seen{$elem}++) {
$youlose = 'yes';
}
It's the same as,
if ($seen{$elem}) {
$youlose = 'yes';
} else {
$seen{$elem} += 1;
}
HTH;
__END__
--
s::a::n->http(www.trabas.com)