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

I get a syntax error when running the following in a subroutine:
if ($_[1] == "1") {push @{{$_[2]}{$_[0]}{$_[1]}}, $_[3];}
Am I not allowed to use multiple symbolic references in an array name?

Replies are listed 'Best First'.
Re: symbolic ref error
by merlyn (Sage) on Aug 10, 2005 at 16:45 UTC
      I know it looks messy but what I am trying to do is put a value into an array that is named after the conditions in which the value was taken. Here is more:
      $seq = 'KLLS3'; $point = 'weight'; $index = 4; $mean = 1.443; sorting($seq, $index, $point, $mean); sub sorting { if ($_[0] eq "KLLS3_LT.V4_0") { if ($_[1] == "1") {push @{{$_[2]}{$_[0]}{$_[1]}}, $_[3];} }
        ...put a value into an array that is named after the conditions...

        Is there any reason why you can't use a hash array instead of a symbolic reference to a regular array (and use strict)? E.g.:

        my $seq = 'KLLS3'; my $point = 'weight'; my $index = 4; my $mean = 1.443; my %hash; sorting($seq, $index, $point, $mean); sub sorting { if ($_[0] eq "KLLS3_LT.V4_0") { if ($_[1] == "1") {push @{$hash{$_[2]}{$_[0]}{$_[1]}}, $_[3];} } }
Re: symbolic ref error
by Ovid (Cardinal) on Aug 10, 2005 at 16:44 UTC

    The error is here:

    {$_[2]}{$_[0]}{$_[1]}

    I don't know what that is supposed to be. However, you might want to consider reformatting this and actually naming your variables. You'll notice the syntax error is here, but it's still much easier to read:

    my ($foo, $bar, $baz, $quux) = @_; if ($bar == 1) { push @{ {$baz}{$foo}{$bar} }, $quux; }

    Cheers,
    Ovid

    New address of my CGI Course.

Re: symbolic ref error
by Transient (Hermit) on Aug 10, 2005 at 16:45 UTC
    ick, why not assign variable names first and see what's going on?
    if ($_[1] == "1") {push @{{$_[2]}{$_[0]}{$_[1]}}, $_[3];}
    to
    my ( $w, $x, $y, $z ) = @_; if ( $x == 1 ) { # or $x eq "1" push @{{$y}{$w}{$x}}, $z; }
    Wait... what does that mean? @{{$y}{$w}{$x}}? Is that supposed to be a HoHoHoA? If so, try
    @{$y->{$w}->{$x}}
Re: symbolic ref error
by neniro (Priest) on Aug 10, 2005 at 17:04 UTC
    Maybe $_[2] or $_[0] aren't defined?!
    if ($_[1] == 1) { die "something went wrong!\n" unless (defined $_[2] and defined $_[0]); # ... }
    See above: 482663 - for a reasonable solution.