in reply to RE: Anonymity
in thread Anonymity
Anonymous hashes and arrays, of course.
Even better is a situation I just came across where I needed to build an array for each key, but I didn't know when I got to a key if I'd seen it before. So I just use
This is a cool example because the %byname hash was built from a hash that had array refs as values, and the arrays were filled with names, possibly duplicates. When I built the %byname hash, the duplicates get eaten up since they "point" to the same key, so that the %final hash looks a bit like the hash %byname was built from, but all the duplicate names are gone.# Rebuild the canonical list, but now we lose duplicates foreach my $name (keys %byname) { # First instance of the id number needs an array ref if (! $final{$byname{$name}}) { $final{$byname{$name}} = [ $name ]; } # Additional instances get pushed onto the existing # (anonymous) array else { push @{$final{$byname{$name}}}, $name; } }
I will be the first to admit that I need to master the
->notation to make my code more readable and intuitive, though :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Anonymous hashes, call me Ishmael.
by frankus (Priest) on Nov 14, 2000 at 16:16 UTC |