edited:
How can I push @myarr into $menu (see below)
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 ]
];
The push() function pushes the new value or values onto the right side of the array and increases the elements. ); push @myNames, 'Moe'; You can also push multiple values onto the array directly ...
Perl | push() Functionpush() function in Perl is used to push a list of values onto the end of the array. push() function is often used with pop to implement stacks. push() function doesn't depend on the type of values passed as list. These values can be alpha-numeric.
The arr. push() method is used to push one or more values into the array. This method changes the length of the array by the number of elements added to the array.
unshift() function in Perl places the given list of elements at the beginning of an array. Thereby shifting all the values in the array by right. Multiple values can be unshift using this operation. This function returns the number of new elements in an array.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With