Why can't I initialize a 2d array like this? (Perl)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lisa L
    New Member
    • Jul 2011
    • 3

    Why can't I initialize a 2d array like this? (Perl)

    Hello all!

    I was wondering why I cannot initialize a 2d array like this?

    my @array1;
    my @array2;
    foreach my $obj (0..$#valueList ){
    $array1[$obj] = ($varible1, $variable2, $variable3);
    $array2[$obj] = ($obj);
    }

    When I try to print the array out by
    foreach my var(@array2){
    print @{$var};
    }

    The printing does not work? Is it impossible to initialize the array like array #2?
  • toolic
    Recognized Expert New Member
    • Sep 2009
    • 70

    #2
    Use square brackets to create a reference:
    Code:
    $array1[$obj] = [($varible1, $variable2, $variable3)];
    In Perl, 2D arrays are known as Arrays-of-Arrays.

    Comment

    Working...