Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Complicated Hash Construction.

by coolmichael (Deacon)
on Dec 29, 2002 at 04:18 UTC ( #222845=perlquestion: print w/ replies, xml ) Need Help??
coolmichael has asked for the wisdom of the Perl Monks concerning the following question:

Why is it that
my %hash; @hash{@keyarray}=@valuearray;
is ok, but combining the two lines
my @hash{@keyarray}=@valuearray;
is a syntax error? I would have expected use strict to generate a "use of uninitialized variable" error or something, but instead, it is a syntax error.

Can anyone please provide some enlightenment?

Comment on Complicated Hash Construction.
Select or Download Code
Re: Complicated Hash Construction.
by Trimbach (Curate) on Dec 29, 2002 at 04:26 UTC
    I always thought it was because in the first example perl knows that %hash is, well, a hash. In the second example perl has no way of knowing whether @hash is a hash or an array. It looks like an array (and the right-hand side is in list context) and the braces look like a syntax error.

    There's no problem when you predeclare %hash because perl knows that @hash{ } is a hash slice and not an array.

    Those more knowledgable about the Perl internals can comment on why this syntax can't be DWIM'd into the interpreter, but I suspect it will not be an issue in Perl 6.

    Gary Blackburn
    Trained Killer

Re: Complicated Hash Construction.
by pg (Canon) on Dec 29, 2002 at 04:33 UTC
    You can declare an entity, but not a member of an entity. This is what the syntax error is about. In your bad case, you tried to declare my @hash{@keyarray}, which is an element of an entity.

    It is NOT about the fact that you combined a declaration with an assignment.

    This is fine:
    my $a = 1;
    and this is not fine:
    my $a[1];
    This is also no good:
    my @a; # this is okay my $a[1];# no good
Re: Complicated Hash Construction.
by MarkM (Curate) on Dec 29, 2002 at 08:14 UTC

    The code:

    my @hash{@keyarray} = @valuearray;

    Should be understood as:

    my($hash{$keyarray[0]}, $hash{$keyarray[1]}, ...) = @valuearray;

    Once this is understood, it should be easier to see why Perl considers the expression to be a syntax error. Perl does not know what my($hash{$key}); means. The exact error is `Can't declare hash slice in "my" at ...'

    For an interesting comparison, consider the following similar-looking code:

    local @hash{@keyarray} = @valuearray;

    This code actually works, because local($hash{$key}); is perfectly valid and means 'override the value of $hash{$key} until this block completes'.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://222845]
Approved by Trimbach
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (16)
As of 2014-09-17 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (90 votes), past polls