I re-wrote your code as shown below. As I think you understand, if you want 9,7 or 3.4 to be grouped together, you need to put those into an anonymous array and put the reference to that array into the simple list, @l. That's what my @l=([9,7],[3,4]); does.
Now to add FOO to @l, you don't need to repackage @l, @l is already a list of references to array (a list of perhaps 2D coordinates). push @p,['FOO',@l]; is the same as push @p,['FOO',[9,7],[3,4]]; I removed the () in the args to push because most of the time, Perl built in functions don't require them.
After the 2nd push, we now have array p which contains, 2 "units" that each consist of a name and some number of
2D coordinates. If we want to get rid of FOO, we have to "unpackage" or dereference and extract the individual
elements of that "unit". Now, the first one can be thrown away. Then we have to "repackage" the elements into
a new anon array ref and pass that modified "unit" to @p @new.
Often in practice, it works out better to package FOO into an anon array even though that is not strictly required. The reason for this is to make every thing in the "unit" the same. I showed that and that making a flat list is now easy because ['FOO'] looks more like one of the coordinates [9,7]. And there is not "if" logic required to skip de-referencing a simple string.
Update: I am going to try again with a box analogy. Instead of thinking x Dimensions, think about boxes and mailing boxes around. If I get this big box @p from you, I open it up and I see that there are 2 boxes inside of it. I open one of these boxes up and see that it has a FOO thing and 2 other boxes. I throw away the FOO thing and repackage the other 2 boxes up into a new box. I never look inside of these 2 smallest boxes. I do the same thing with the second "big" box from within the single @p box. At the end, I wind up with an @new thing that contains 2 boxes. Maybe this analogy is not perfect, but I did try to explain things in a different way for you.use strict; use warnings; use Data::Dump qw(dump dd); my @p=(); my @l=([9,7],[3,4]); # array of 2 coodinates push @p,['FOO',@l]; # array with the string FOO with those coordinat +es push @p,['BAR',[8,0]];# BAR and one coordinate dump \@p; #[["FOO", [9, 7], [3, 4]], ["BAR", [8, 0]]] #note outermost brackets are artifact of way we called dump + as #a reference to an array # throw away FOO and BAR # my @new =map{my @elements = @$_; #This gets "FOO", [9, 7], [3, 4] shift @elements; #throw away FOO [@elements]}@p; #repackage array inside of brackets #back to being a reference to array dump \@new ; # [[[9, 7], [3, 4]], [[8, 0]]] # often it is convienent to make all elements to the structure the sam +e # here packaging the string foo as a single element of an array ref is + fine. my @x; push @x,[['FOO'],@l]; # array with the string FOO with those coordin +ates push @x,[['BAR'],[8,0]];# BAR and one coordinate my @flat = map{@$_}map{@$_}@x; print "@flat\n"; # FOO 9 7 3 4 BAR 8 0
In reply to Re: Cannot have plain/simple array after removal one inside another
by Marshall
in thread Cannot have plain/simple array after removal one inside another
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |