arrays - 将数组插入数组 perl

标签 arrays perl multidimensional-array

很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center .




8年前关闭。




编辑:

如何将@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 ]
     ];

最佳答案

这取决于您究竟想做什么。

您可以直接推送数组:

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

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

这实际上插入了数组(嵌套数组)。

关于arrays - 将数组插入数组 perl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13133587/

相关文章:

arrays - 如何在swift中将字符串转换为 float 组

perl - 如何在 Perl Pod 的链接中添加格式?

regex - 从通过管道传输到 Perl 的多行文本中提取字段

java - 在Java中打印二维数组 radio

php - 根据值从多维数组中删除元素

java - 使用快速排序按列对二维数组进行排序

arrays - $firebaseArray 中的 For 循环

java - 字符串数组的减法

javascript - JavaScript 中多个数组的笛卡尔积

perl - 如何将命令行参数传递给 Perl 程序?