[kw-pm] a philosophical question about hashes

John Macdonald john at perlwolf.com
Thu Jun 16 10:49:14 PDT 2005


On Thu, Jun 16, 2005 at 01:30:11PM -0400, Robert P. J. Day wrote:
> On Thu, 16 Jun 2005, John Macdonald wrote:
> 
> > There are certainly times when having the key inside the value is
> > useful.  If you have a subroutine that operates on a single value
> > record, it could easily need to know the value of the key as well as
> > the others parts of the record.  It's easier to pass it a single
> > coherent value than to have to pass the key as well.
> >
> >     process_record( $structure{$_} ) for (@interesting_keys);
> 
> in the general case, of course, if i want multiple keys, i'd create a
> hash value structure that contained all of those key values, then just
> create distinct hashes for each key.  but i don't think i need to get
> that carried away just yet.

That would depend upon whether the list of keys had long
term significance or whether they just happened to be of
momentary interest.  (Instead of "@interesting_keys" you might
have "grep /foo.*bar/, keys %structure" as the for iterator.)
Regardless, my point was that just as a select from a database
returns complete records, *including the key field(s)*, it is
useful for the value object to contain the complete record if
it is a useful data structure in its own right.

-- 


More information about the kw-pm mailing list