Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Adding an new element to array.

by Anonymous Monk
on Aug 21, 2013 at 15:34 UTC ( [id://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!

Replies are listed 'Best First'.
Re: Adding an new element to array.
by toolic (Bishop) 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 marinersk (Priest) 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 hdb (Monsignor) 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 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found