mr_p has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am not able to figure out the current syntax for the following error. Please help.

my (@isec , @b ) = (); (@b, @isec) = ArrayFunctions(\@ArrayA, \@ArrayB, \@b, \@isec); sub ArrayFunctions { my $a_ref = shift; # reference to input array A my @a = @$a_ref; # input array A my $b_ref = shift; # reference to input array B my @b = @$b_ref; # input array B my $b = shift; # input array Bonly my $isec = shift; # input array isec_plus_bonly my @Aseen{@a} = (); # lookup table my @Bseen{@b} = (); # lookup table #.......... Other Code ..... }

Syntax Error:

syntax error at ./dedup1.pl line 39, near "@Aseen{" Execution of ./dedup1.pl aborted due to compilation errors.

Replies are listed 'Best First'.
Re: Syntax error for the following ???
by Corion (Patriarch) on Mar 30, 2010 at 17:57 UTC

    You can't do

    my @Aseen{...} = ...;

    which is what Perl tells you. Split it up into two steps and Perl will understand you:

    my %Aseen; @Aseen{ @a } = ()

    Also, why are you copying the values from @$a_ref into @a just to use it to assign to %Aseen ? Why not use them directly for assigning?

    @Aseen{ @$a_ref } = ()

      Worked. Thanks.

      Don't know how it worked without use strict.

        Also note that in the statement
            (@b,@isec) = ArrayFunctions(\@ArrayA, \@ArrayB, \@b, \@isec);
        the array  @b will consume the entire list returned by the  ArrayFunctions() invocation, leaving  @isec eternally empty. This is due to the 'list flattening' behavior of Perl.

        It doesn't work whether use strict; is used or not.

        $ perl -e'my @Aseen{@a} = ();' syntax error at -e line 1, near "@Aseen{" Execution of -e aborted due to compilation errors.

        You probably added that my at the same time you added use strict;

Re: Syntax error for the following ???
by GrandFather (Saint) on Mar 30, 2010 at 20:46 UTC

    You don't need to set arrays to empty at declaration time - they start out that way.

    Rather than using shift to pull off parameters one at a time in various scattered locations through your sub, use list assignment with a declaration of a list of variables at the top of the sub.

    If you have a reasonable variable name adding a comment that simply reiterates the variable name in expanded form doesn't add any information.

    Taking all that into account your code sample becomes:

    my @isec; my @b; (@b, @isec) = ArrayFunctions(\@ArrayA, \@ArrayB, \@b, \@isec); sub ArrayFunctions { my ($a_ref, $b_ref, $b, $isec) = @_; my @a = @$a_ref; my @b = @$b_ref; my %Aseen = map {$_ => undef} @a; my %Bseen = map {$_ => undef} @b; #.......... Other Code ..... }

    Oh, and misleading comments are worse than no comment. There was a mismatch between the names of the hashes in the sub and the comment so I removed the comment. If the comment was right and names are wrong, change the names (and still omit the comment).


    True laziness is hard work