Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Adding an new element to array.

by Anonymous Monk
on Aug 21, 2013 at 15:34 UTC ( #1050374=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to add one element "DOC => 'EMAIl'" from $VAR2 to $VAR1,
$VAR1 = { 'DATE' => '2013-04-25', 'TW' => '1', 'REF' => '1234567', 'ACC' => '33456790' }; $VAR2 = { 'DOC' => 'EMAIL' };
but I am stuck, I tried this:
push @$data, {DOC => 'EMAIL'};
No success.

I would like to have this at the end:
$VAR1 = { 'DATE' => '2013-04-25', 'TW' => '1', 'REF' => '1234567', 'ACC' => '33456790' 'DOC' => 'EMAIL' };

Thanks for the help!

Comment on Adding an new element to array.
Select or Download Code
Re: Adding an new element to array.
by toolic (Chancellor) on Aug 21, 2013 at 15:38 UTC
    You have a reference to a hash, and you just want to add a new key to the hash:
    use warnings; use strict; use Data::Dumper; $Data::Dumper::Sortkeys=1; my $data = { 'DATE' => '2013-04-25', 'TW' => '1', 'REF' => '1234567', 'ACC' => '33456790' }; $data->{DOC} = 'EMAIL'; print Dumper($data); __END__ $VAR1 = { 'ACC' => '33456790', 'DATE' => '2013-04-25', 'DOC' => 'EMAIL', 'REF' => '1234567', 'TW' => '1' };

      The data is a array of hashes:
      my @new; foreach my $row (@$data) { push @new, DOC => "EMAIL"}; # } push @$data, @$new;
Re: Adding an new element to array.
by hdb (Prior) on Aug 21, 2013 at 15:39 UTC

    How about:

    @$VAR1{ keys %$VAR2 } = values %$VAR2;

    or just

    $VAR1->{'DOC'} = $VAR2->{'DOC'};
Re: Adding an new element to array.
by marinersk (Chaplain) on Aug 21, 2013 at 16:03 UTC
    Adding an new element to array
    Just so you know, this is not an array. It is a hash.

    Worse, it is a reference to a hash, not a directly specified hash the way most people start using them.

    Looks like others are giving guidance on how you might update that hash, so I won't replicate that information here. But nobody seems to have mentioned that you appear to be starting from a faulty assumption: You do not have an array in the usual Perl sense of the word.

Re: Adding an new element to array.
by protist (Monk) on Aug 21, 2013 at 17:58 UTC

    Here is one good way to do it, but as always, tim toady.

    use warnings; use strict; use Data::Dumper; $Data::Dumper::Sortkeys=1; my $data = { 'DATE' => '2013-04-25', 'TW' => '1', 'REF' => '1234567', 'ACC' => '33456790' }; $data = {%$data, DOC => 'EMAIL'}; print Dumper($data);

    Alternatively, to add the second hash to the first, do something like this:

    use warnings; use strict; use Data::Dumper; $Data::Dumper::Sortkeys=1; my $data = { 'DATE' => '2013-04-25', 'TW' => '1', 'REF' => '1234567', 'ACC' => '33456790' }; my $new = { DOC => 'EMAIL' ## NOTE: no need for quotes on thing ## to the left of `=>' }; $data = {%$data, %$new}; print Dumper($data);
      OK may be I am not been clear but this is the reason:
      my $data = $dbh->exec("select * ......"); if($data) { my $more_data = $dbh ->exec("select * ...."); if($more_data) { my $name = $more_data->[0]{ NAME }; push @{$data}, {doc_type => $name}; } } print Dumper(@$data);
      Thanks!
        #!perl use strict; use Data::Dump 'pp'; my $data = [{ 'DATE' => '2013-04-25', 'TW' => '1','REF' => '1234567', 'ACC' => '33456790' }]; my $more_data = [{'DOC1' => 'EMAIL1','DOC2' => 'EMAIL2'}]; my @keys = keys %{$more_data->[0]}; @{$data->[0]}{@keys} = @{$more_data->[0]}{@keys}; pp @$data;
        poj

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2014-09-16 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (51 votes), past polls