pushing array inside array perl

arrays, multidimensional-array, perl

Solution

This depends on what exactly you want to do.

You can either directly push the array:

push (@$menu, @myarr);

#results in:

[
     "List",
     ["itemone", \&ds2],
     ["itemtwo", \&ds2],
     ["itemthree", \&ds2],
     ["itemfour", \&ds2],
     [ "Do Something (second)", \&ds2 ],
     [ "itemone", "itemoneb", "itemonec" ],
     [ "itemtwo", "itemtwob", "itemtwoc" ],
     [ "itemthree", "itemthewwb", "itemthreec" ],
     [ "itemfour", "itemfourb", "itemfourc" ]
];

which results in the `myarr` elements being pushed to `menu`, or push the reference:

push (@$menu, \@myarr);

#results in:

[
     "List",
     ["itemone", \&ds2],
     ["itemtwo", \&ds2],
     ["itemthree", \&ds2],
     ["itemfour", \&ds2],
     [ "Do Something (second)", \&ds2 ],
     [
        [ "itemone", "itemoneb", "itemonec" ],
        [ "itemtwo", "itemtwob", "itemtwoc" ],
        [ "itemthree", "itemthewwb", "itemthreec" ],
        [ "itemfour", "itemfourb", "itemfourc" ],
     ],
];

which actually pushes the array (nested array).

Problem

edited: How can I push @myarr into $menu (see below) ``` my @myarr = ( [ "itemone", "itemoneb", "itemonec" ], [ "itemtwo", "itemtwob", "itemtwoc" ], [ "itemthree", "itemthewwb", "itemthreec" ], [ "itemfour", "itemfourb", "itemfourc" ] ); $menu = [ "List", ["itemone", \&ds2], ["itemtwo", \&ds2], ["itemthree", \&ds2], ["itemfour", \&ds2], [ "Do Something (second)", \&ds2 ] ]; ```

Original source