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

Hi Monks

Array contains

@arr=(["one" => [1,11]], ["two"=> [2,22]]);

I need to insert 222 in second Element.

Needed output

@arr=(["one" => [1,11]], ["two"=> [2,22,222]]);

please guide me.

Note: Index position may vary.

Thanks,
Kanishk

Replies are listed 'Best First'.
Re: insert value into an array
by ayrnieu (Beadle) on Feb 14, 2006 at 09:45 UTC
    sub alist_get { my $k = shift; for (@_) { return $_ if $_->[0] eq $k } die "No such key in alist: $k" } push @{alist_get('two', @arr)->[1]}, 222;

    Incidentally, => works just like a comma, except that it stringifies its LHS. It's not hash-related magic. So, you could define your array thus:

    my @a = ([one => [1,11]], [two => [2,22]]);

    And likewise I could've invoked my subroutine as alist_get(two => @arr) -- Neat, huh?

    /assumes you have a good reason to not use a hash. :-)

Re: insert value into an array
by Samy_rio (Vicar) on Feb 14, 2006 at 09:48 UTC

    Hi, Try this,

    use strict; use warnings; use Data::Dumper; my @arr=(["one" => [1,11]], ["two"=> [2,22]]); my ($position, $value) = @ARGV; print "\nBefore:\n"; print Dumper @arr; push (@{$arr[$position]->[1]}, $value); print "\nAfter:\n"; print Dumper @arr; __END__ Before: $VAR1 = [ 'one', [ 1, 11 ] ]; $VAR2 = [ 'two', [ 2, 22 ] ]; After: $VAR1 = [ 'one', [ 1, 11 ] ]; $VAR2 = [ 'two', [ 2, 22, '222' ] ];

    If you need the value as integer then use int function as push (@{$arr[$position]->[1]}, int($value));

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: insert value into an array
by turo (Friar) on Feb 14, 2006 at 09:51 UTC

    I think is enough with this:

    push @{$arr[1]->[1]},222;

    cheers

    turo
    perl -Te 'print map { chr((ord)-((10,20,2,7)[$i++])) } split //,"turo"'
Re: insert value into an array
by GrandFather (Saint) on Feb 14, 2006 at 09:57 UTC

    Are you sure you are asking the right question and that the code represents what you think that it does? The following code does what you ask for and dumps the result:

    use warnings; use strict; use Data::Dump::Streamer; my @arr=(["one" => [1,11]], ["two"=> [2,22]]); push @{$arr[1][1]}, 222; Dump (\@arr);

    Prints:

    $ARRAY1 = [ [ 'one', [ 1, 11 ] ], [ 'two', [ 2, 22, 222 ] ] ];

    However, I suspect that what you want is not an AoAoA, but a HoA, in which case it all looks like this:

    use warnings; use strict; use Data::Dump::Streamer; my %arr= ("one" => [1,11], "two"=> [2,22]); push @{$arr{'two'}}, 222; Dump (\%arr);

    Prints:

    $HASH1 = { one => [ 1, 11 ], two => [ 2, 22, 222 ] };

    DWIM is Perl's answer to Gödel
Re: insert value into an array
by inman (Curate) on Feb 14, 2006 at 09:54 UTC
    You are just wanting to push the value onto the appropriately dereferenced list.
    push @{$arr[1][1]}, 222;
    As with some of the other comments, the declaration of the structure leads me to believe that the OP was trying to specify a hash of lists. e.g.
    my %hoa=("one" => [1,11], "two"=> [2,22]);
    in which case
    push @{$hoa{two}}, 222;
Re: insert value into an array
by vennirajan (Friar) on Feb 14, 2006 at 09:57 UTC
    Hi,
    You can use the following piece of code.
    push @{${$arr[1]}[1]},222;

    Hope it Helps !!!

    Regards,
    S.Venni Rajan.
    "A Flair For Excellence."
                    -- BK Systems.