Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pushing array inside array perl [closed]

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 ]
     ];
like image 457
user391986 Avatar asked Oct 30 '12 06:10

user391986


People also ask

How do I push an array to an array in Perl?

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 ...

How do I push an array of data in Perl?

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.

How do you push inside an array?

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.

What does Unshift do in Perl?

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.


1 Answers

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

like image 198
Christoph Avatar answered Oct 12 '22 23:10

Christoph