Trina Espinoza wrote:
> I have a hash that contains keys, but does not yet have
> values. 

Well, that's not technically possible. Each key is associated with a single
scalar value. The only way to create a key is to assign a value.

> How do you push a list of an item into the value for a
> particular key? 
> 
> I tried doing this example below, but it does not seem to be
> working. Is there anything wrong with the way this is written? Can
> someone give me suggestions for trouble shooting if this looks fine?
> 
> 
> Thanks!
> 
> -T
> ----------------------------------------------------------
>  unless (exists $shots_OTIS{$shot})  {
>    #print "putting shot in array";
>    $shots_OTIS{$shot} = "";

This is the problem. You initialize the value to "" (a simple scalar), and
then try to use that as a reference later. You need to initialize the value
to a reference to an empty array:

   $shots_OTIS{$shot} = [];

The brackets create an anonymous (empty) array and return a reference to it.

>   }
>   push( @{$shots_OTIS{$shot}}, $endShot); ###TRIED THIS. DID NO WORK
> 
> 
>  }

But, you don't need to do this at all. Perl has something called
"autovivification", which means that when you say:

   push @{$shots_OTIS{$shot}}, $endShot;

The reference $shots_OTIS{$shot} is automatically created if it doesn't
exist. So you can bypass all the exists() checking and just use the push by
itself.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to