use*_*986 4 arrays perl multidimensional-array
编辑:
我如何将@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 ]
];
Run Code Online (Sandbox Code Playgroud)
这取决于您到底想做什么。
您可以直接推送数组:
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" ]
];
Run Code Online (Sandbox Code Playgroud)
这导致myarr
元素被推到menu
或推引用:
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" ],
],
];
Run Code Online (Sandbox Code Playgroud)
实际上推动了数组(嵌套数组)。
你可以推它:
use Data::Dumper;
push (@$menu, @myarr);
print Dumper($menu), "\n";
Run Code Online (Sandbox Code Playgroud)