Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 将数组推入数组perl_Arrays_Perl_Multidimensional Array - Fatal编程技术网

Arrays 将数组推入数组perl

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

编辑:

如何将@myarr推入$menu(见下文)

您只需按一下即可:

 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" ],
     ],
];