SPUG: HASH help

ced at carios2.ca.boeing.com ced at carios2.ca.boeing.com
Thu Jul 25 17:12:39 CDT 2002


To: spug-list at pm.org, stephen.m.baker at intel.com
Subject: Re: SPUG: HASH help

> I need to make a multi-dimensional hash.  The top level has "$user" as keys,
> and the values are references to another hash, hash2.  Hash2 has "$item" as
> keys, and the values are references to an array.  The array has any number
> of element in it... the goal being to be able to create a portable database
> wherein the following is possible:

>        by specifying a user, you can look to see the various items
> associated with them, and then get specifics about any of those items...

> I need a starting point... I'm somewhat familiar with perl but if anyone
> could give me some direction that would be helpful.

You can get a feel for Perl data structures by looking at the docs 
(perllol, perldata, perlref for example). Most of the recommended
books deal with this in depth.
  
Here're a few specific items:  

my %people;
  ....
# step 1: create  an array ref. within the data structure  
  $people{$user}{$item} = [];  set up an anonymous array   

# step 2: populate above array
  push( @{ $people{$user}{$item} }, 'value1'; 
  push( @{ $people{$user}{$item} }, 'value2'; 

  #  prettier alteratives for step 2:
  $people{$user}{$item}->[0] = 'value1'; 
  $people{$user}{$item}->[1] = 'value1'; 
  
  my @array = qw( value1 value2 );
  $people{$user}{$item} = \@array;


but, note, you could leave out 'step 1' altogether  and Perl will 
create the array references for you at 'step 2'.   

Rgds,
--
Charles DeRykus

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list