On Fri, 12 Mar 2004, R. Joseph Newton wrote:

>
> Charlotte Hee wrote:
>
> >
> > 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 ) {
>
> So all the records differ only in name, and all other parameters stay the same?
>
> >     $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...".
>
> Since you don't seem to be showing us the code you are actually using, we are
> somewhat at a disadvantage.  ONe thing you should not, though.  Since the nested
> hashes should be storedonly by reference, you should use the derefereing operator
> -> to get at least the fianl element.
>
> >  $byname{ Owen }{ PHONE } = '999-9999';
>
> Should be:
> $byname{ Owen }->{ PHONE } = '999-9999';
> are you using strict?  The code above should cause an error, not just an
> uninitialized variable warning.  You really should put:
> use strict;
> use warnings;
> at the top of the script, and clean up the errors returned before you try to take
> on multidimensional structure problems.  Houses built on sand cannot be expected
> to stand.
>
> Joseph
>

Ah, I see how the syntax works. Assume that %byname is a hash of record
datatypes (this is like the perl cookbook example) and has predefined
values from somewhere. Adding the phone number after %byname has been
populated with data I could add the phone like this:

$num = 1234567;  # fake phone number for testing.

foreach $item ( keys %byname ){
    $rp = $byname{$item};
    $rp->{PHONE} = $num;
    ++$num;
}

or I could add the phone number this way:

$num = 1234567;  # fake phone number for testing.

foreach $item ( keys %byname ){
    $byname{$item}->{PHONE} = $num;
    ++$num;
}

Seeing your responses about the use of the arrow notation somehow made
things click in my head.

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>


Reply via email to