5

编辑:

如何将@myarr 推送到 $menu 中(见下文)

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 ]
     ];
4

2 回答 2

7

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).

于 2012-10-30T15:39:42.750 回答
5

你可以推它:

 use Data::Dumper;
 push (@$menu, @myarr);
 print Dumper($menu), "\n";
于 2012-10-30T06:18:41.193 回答