Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Push function

by boom (Scribe)
on Apr 06, 2009 at 05:31 UTC ( [id://755637]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,
my @arr = ([11,22,[[]]],[333,444,[]],[6666,7777,[]]);

In the above code I need to push a value into the $arr[0]'s 3rd element's anonymous array. How can I do it ?

Replies are listed 'Best First'.
Re: Push function
by manoj_speed (Prior) on Apr 06, 2009 at 05:36 UTC
    You can use this to achieve it.
    use Data::Dumper; my @arr = ([11,22,[[]]],[333,444,[]],[6666,7777,[]]); push(@{$arr[0][2][0]},3); print Dumper @arr;
      Hi manoj_speed,

      Is it possible to push a value into anonymous array without dereference it

        No.

        "How do I make a phone call to England?" - "Here you go, you take the phone and dial..." - "can I do that without a phone?".

Re: Push function
by leslie (Pilgrim) on Apr 06, 2009 at 05:47 UTC
    You can do like this.
    my @arr = ([11,22,[ []]],[333,444,[]],[6666,7777,[]]); print Dumper @arr; push (@{$arr[0]->[2]->[0]},3); print Dumper @arr;
Re: Push function
by GrandFather (Saint) on Apr 06, 2009 at 20:51 UTC

    Note that you don't need to 'make space'. Consider:

    use strict; use warnings; use Data::Dump::Streamer; my @arr = ([11, 22, [[]]], [333, 444, []], [6666, 7777, []]); push @{$arr[0][2][0]}, 3; Dump \@arr; @arr = ([11, 22], [333, 444], [6666, 7777]); push @{$arr[0][2][0]}, 3; Dump \@arr;

    Prints:

    $ARRAY1 = [ [ 11, 22, [ [ 3 ] ] ], [ 333, 444, [] ], [ 6666, 7777, [] ] ]; $ARRAY1 = [ [ 11, 22, [ [ 3 ] ] ], [ 333, 444 ], [ 6666, 7777 ] ];

    True laziness is hard work
Re: Push function
by bichonfrise74 (Vicar) on Apr 06, 2009 at 20:03 UTC
    Hello, I wrote it in this manner... Although the code looks ugly, I find it more readable. Is this a bad practice though?

    #!/usr/bin/perl use strict; use Data::Dumper; my @arr = ( [11,22,[[]]], [333,444,[]], [6666,7777,[]] ); push ( @{ @{ @{ @arr }[0]->[2] }[0] }, 'bbb' ); print Dumper(\@arr);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-24 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found