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

Hi Monks, I have an array @msgs. I want this array to hold arrays containing $msglines.

@msgs=(@msgs[0],@msgs1,@msgs2) where @msgs[0],@msgs1,@msgs2 are arrays of type $msglines.

How do I push $msglines to @msgs[0]?

My understanding was that array of arrays stores references to subarrays. So its illegal to say
@msgs=(); push(@msgs[0],$msglines); print "Size of first array ".($#(@($msgs[0]))+1)."\n";
Needless to say above code doesnot work :(. How else to append to arrays inside arrays?

Replies are listed 'Best First'.
Re: Push into an array inside a array
by dasgar (Priest) on Sep 11, 2010 at 23:49 UTC

    This is topic covered in perllol. Using that reference, here's a sample script that does what you're looking to do.

    use strict; use Data::Dumper; my @array; push @{$array[0]},[0,1,2]; print Dumper @array;

    Here's the output:

    $VAR1 = [ [ 0, 1, 2 ] ];
Re: Push into an array inside a array
by eyepopslikeamosquito (Archbishop) on Sep 12, 2010 at 00:26 UTC

    Others have already pointed you at parts of the Perl documentation you need to read. Though your test code doesn't compile even without strict and warnings, please note for future reference that you should always turn on strict and warnings, especially if you're unsure of the syntax. In your example test code, doing that produces an extra warning, namely:

    Scalar value @msgs[0] better written as $msgs[0] at f1.pl line 5.
    This is such a common Perl syntactic oversight that use warnings helpfully warns you of it. For more information on this particular common coding mistake, please see Common problems with slices from the excellent series of Perl tips provided by Perl Training Australia.

    If I understand you correctly, you want something like:

    use strict; use warnings; use Data::Dumper; my @msgs; push @msgs, [ "1st array: line 1", "1st array: line 2" ]; push @msgs, [ "2nd array: line 1", "2nd array: line 2", "2nd array: li +ne 3" ]; print Dumper(\@msgs); my $i = 0; for my $arr (@msgs) { ++$i; print "array $i:\n"; for my $line (@{$arr}) { print " $line\n"; } }

Re: Push into an array inside a array
by toolic (Bishop) on Sep 11, 2010 at 23:46 UTC
    Maybe this will help:
    use strict; use warnings; use Data::Dumper; # $msglines is a reference to an array my $msglines = [ qw(a b c) ]; # This pushes an array ref into an array, # creating an Array-of-arrays data structure my @msgs; push @msgs, $msglines; print Dumper(\@msgs); print "Size of first array ", scalar @{ $msgs[0] }, "\n"; __END__ $VAR1 = [ [ 'a', 'b', 'c' ] ]; Size of first array 3

    See also:

Re: Push into an array inside a array
by BrowserUk (Patriarch) on Sep 12, 2010 at 00:23 UTC

    Assuming that mean push a scalar onto an arrayref already contained within an array element, (as opposed to pushing an arrayref onto an array as the other respondents have demonstrated), then:

    @a = ( [1, 2.3 ], ['a'..'c'], ['p'..'r']);; pp \@a;; [[1, "2.3"], ["a", "b", "c"], ["p", "q", "r"]] push @{ $a[ 1 ] }, 'a scalar';; pp \@a;; [[1, "2.3"], ["a", "b", "c", "a scalar"], ["p", "q", "r"]]

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.