Arrays 将数组推入数组perl
编辑: 如何将@myarr推入$menu(见下文) 您只需按一下即可:Arrays 将数组推入数组perl,arrays,perl,multidimensional-array,Arrays,Perl,Multidimensional Array,编辑: 如何将@myarr推入$menu(见下文) 您只需按一下即可: use Data::Dumper; push (@$menu, @myarr); print Dumper($menu), "\n"; 这取决于你到底想做什么 您可以直接推送阵列: push (@$menu, @myarr); #results in: [ "List", ["itemone", \&ds2], ["itemtwo", \&ds2], ["i
use Data::Dumper;
push (@$menu, @myarr);
print Dumper($menu), "\n";
这取决于你到底想做什么 您可以直接推送阵列:
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" ]
];
这将导致myarr
元素被推送到菜单
,或推送引用:
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" ],
],
];
它实际上会推送数组(嵌套数组)。这会将
@myarr
的元素推送到@$menu
中,以推送数组推送其引用push$@menu,\@myarr
您尝试了什么,结果是什么?阅读后不应该那么难。:)您是否正在尝试将“itemoneb”和“itemonec”推送到持有“itemone”的现有arrayref中?什么是&ds2
?为什么这不是一个真正的问题。问题很清楚。
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" ],
],
];