On Thu, 11 Mar 2004, John W. Krahn wrote:
> Date: Thu, 11 Mar 2004 16:33:00 -0800 > From: John W. Krahn <[EMAIL PROTECTED]> > To: [EMAIL PROTECTED] > Subject: Re: not getting the syntax > > Charlotte Hee wrote: > > > > Hello, > > Hello, > > > I'm looking at the perl cookbook example on how to create a record data > > type: > > > > $record = { > > NAME => "Jason", > > EMPNO => 132, > > TITLE => "deputy peon", > > AGE => 23, > > SALARY => 37_000, > > PALS => [ "Norbert", "Rhys", "Phineas"], > > }; > > > > # store record > > $byname{ $record->{NAME} } = $record; > > > > Is it possible to add another key:value-pair to $record *after* its > > already been saved in %byname? For example, if I want to add > > 'phone => 999-9999' to $record for Jason. How do I do that? > > > > I tried the following but it doesn't work and gives an error. > > > > $byname { $record->{"Jason"}->PHONE } = '999-9999'; # error undefined val > > > > I keep looking at the syntax but it's not sinking in. > > > $record is a reference to a hash so just add it directly to $record: > > $record->{ PHONE } = '999-9999'; > > > Or to access it via the %byname hash: > > $byname{ Jason }{ PHONE } = '999-9999'; > > > > John > -- > use Perl; > program > fulfillment > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > <http://learn.perl.org/> <http://learn.perl.org/first-response> > > > For a single record I can see how that works but let's say I have 4 or 5 employees and I have the employee information for each one (assumed). Now I want to build a record for each employee in a loop like this: @names = ('Jason','Aria','Samir','Owen'); foreach $na ( @names ) { $record = { NAME => $na, EMPNO => $emp_no, TITLE => $title, AGE => $age, SALARY => $salary, PALS => [ $friend_list ], }; # store record $byname{ $record->{NAME} } = $record; } Now I want to add something later, after the record for the employee has been created. For example, I want to add the phone for Owen. When I try the following I get "can't use undefined value...". $byname{ Owen }{ PHONE } = '999-9999'; thanks, Chee -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>