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

Hi ,
How can I push an array to the below structure

use Data::Dumper; $form->{days} = [ '2011-02-26', '2011-02-25', '2011-02-24', '2011-02-23', '2011-02-22', '2011-02-21', '2011-02-20', ];

Need to push statement
my @extra_date = ('2011-03-01','2011-02-28','2011-02-27');
and expecting result when I do Dumper as
[ '2011-03-01', '2011-02-28', '2011-02-27' '2011-02-26', '2011-02-25', '2011-02-24', '2011-02-23', '2011-02-22', '2011-02-21', '2011-02-20', ];
Thanks

Replies are listed 'Best First'.
Re: Push Array
by Nikhil Jain (Monk) on Mar 23, 2011 at 10:40 UTC
    use unshift for Prepends list to the front of the array.
    use strict; use Data::Dumper; my $form = {}; $form->{days} = [ '2011-02-26', '2011-02-25', '2011-02-24', '2011-02-23', '2011-02-22', '2011-02-21', '2011-02-20', ]; my @extra_date = ('2011-03-01','2011-02-28','2011-02-27'); unshift(@{$form->{days}}, @extra_date); print Dumper($form->{days});

    Output:

    $VAR1 = [ '2011-03-01', '2011-02-28', '2011-02-27', '2011-02-26', '2011-02-25', '2011-02-24', '2011-02-23', '2011-02-22', '2011-02-21', '2011-02-20' ];
      Thanks for the information.
Re: Push Array
by wind (Priest) on Mar 23, 2011 at 17:00 UTC

    As already explained, unshift is your answer, along with shift, push, and pop.

    For a style hint, I'd just like to point the use of qw.

    use Data::Dumper; use strict; my $form = {days => [qw( 2011-02-26 2011-02-25 2011-02-24 2011-02-23 2011-02-22 2011-02-21 2011-02-20 )]}; unshift @{$form->{days}}, qw(2011-03-01 2011-02-28 2011-02-27); print Dumper($form);