Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Cannot have plain/simple array after removal one inside another

by Anonymous Monk
on Dec 28, 2021 at 00:13 UTC ( [id://11139965]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do we have plain array after ones in multi dimensional array are removed ?
@p=(); my @l=([9,7],[3,4]); push(@p,['FOO',[@l]]); push(@p,['BAR',[[8,0]]]); splice(@$_,0,1) for @p; print "\n",@$_ for @p; ARRAY(0x5624cf01e340) ARRAY(0x5624cf028d78)

How do we have it simple/plain 2D array with 1x2 in inner/2nd D. one

Replies are listed 'Best First'.
Re: Cannot have plain/simple array after removal one inside another
by dsheroh (Monsignor) on Dec 28, 2021 at 09:43 UTC
Re: Cannot have plain/simple array after removal one inside another
by LanX (Saint) on Dec 28, 2021 at 00:26 UTC
    this push(@p,['FOO',[@l]]); and push(@p,['BAR',[[8,0]]]); are pushing 3D-arrays, just count the brackets.

    hence @p will be 4D

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Cannot have plain/simple array after removal one inside another
by Marshall (Canon) on Dec 29, 2021 at 09:00 UTC
    I agree that it is not clear what you are trying to do. However some general points about references may help you.

    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.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11139965]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-26 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found