agoth has asked for the wisdom of the Perl Monks concerning the following question:
I was using the ternary syntax but it didnt work like I expected. Why does the second construct (without the braces) clobber the hash key with the value???
my %hash = ('apple2' => 'green', 'apple1' => 'green', 'banana' => 'yellow', 'plum' => 'red'); my %distinct; my %overflow; while (my ($k, $v) = each %hash) { # construct1 # (defined $distinct{$v}) ? ($overflow{$k} = $v) # : ($distinct{$v} = $k); # construct2 (exists $distinct{$v}) ? $overflow{$k} = $v : $distinct{$v} = $k; } for (keys %overflow) { print "$_, $overflow{$_} \n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re: Recipe of day - hashes q.
by tye (Sage) on Oct 16, 2000 at 22:00 UTC | |
|
Re: Recipe of day - hashes q.
by swiftone (Curate) on Oct 16, 2000 at 21:32 UTC |