> -----Original Message-----
> From: John Edwards [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, August 23, 2001 10:15 AM
> To: Perl Beginners (E-mail)
> Subject: Append array in hash of arrays under strict
>
>
> Here's some sample code
>
> ---
> use strict;
>
> my (%hash, @array, @results);
>
> $hash{'name'}{'sub'} = qw(one two three four);
This line isn't doing what you think it is. Step through this in the
debugger and see what $hash{name}{sub} is. It's 'four'.
Remember, a hash entry can only hold a scalar. You are tyring to assign
a list to a scalar. From perldoc perldata, a list evaluated in scalar
context returns the last element in the list ('four').
To put a list into a hash entry, you need to construct an anonymous
array and store a reference to that array. the [ ] anonymous array
constructor does the trick:
$hash{name}{sub} = [ qw(one two three four) ];
>
> @array = qw(five six seven eight);
>
> $hash{'name'}{'sub'} = ($hash{'name'}{'sub'}, @array);
Same problem as above, but this time, the last element of
the list (@array) is evaluated in scalar context, which
results in the number of elements in @array, e.g. 4.
Also, $hash{name}{sub} on the right-hand side is an array
ref (if you use the [ ] above), so you need to dereference
it:
$hash{name}{sub} = [ @{$hash{name}{sub}}, @array ];
>
> print @{$hash{'name'}{'sub'}};
> ---
>
> This produces an error "Can't use string ("4") as an ARRAY
> ref while "strict
> refs" in use at test.pl line 11."
>
> The Perl Cookbook says this "causes a runtime exception under
> use strict
> because you're dereferencing an undefined reference where
> autovivification
> won't occur" and suggests using...
Not quite. You are not dereferencing an undefined ref, you
are trying to dereference something which is not a reference
at all.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]