how do i push a hashref into another hashref
how do i push a hashref into another hashref
am 07.06.2011 08:47:55 von Agnello George
--20cf303dd4fc8275b104a5199977
Content-Type: text/plain; charset=ISO-8859-1
HI
I got the following hashref
my $selet_domU_data = $DBH->selectall_hashref("select
ram,ip,application,hosting,assigned_to,rdom0id from domU_info where
server_name='$domU_server' ",'server_name' );
my $select_all_website = $DBH->selectall_hashref("select
website_id,website_name from websites_name ",'website_id');
now i need to push $select_all_website into $selet_domU_data
my %hash1 = %$select_all_website;
foreach (keys %$selet_domU_data) {
push (@{$selet_domU_data->{$_}}, { rets => %hash1 );
}
print Dumper ([$selet_domU_data]);
i also tried a combination of many other things but does not seem to work
thanks in advanced
--
Regards
Agnello D'souza
--20cf303dd4fc8275b104a5199977--
Re: how do i push a hashref into another hashref
am 07.06.2011 09:43:17 von oleber
--0023543a2b92e8d1d104a51a610f
Content-Type: text/plain; charset=ISO-8859-1
I don't understand what you are trying to do
On Tue, Jun 7, 2011 at 08:47, Agnello George wrote:
> HI
>
> I got the following hashref
>
> my $selet_domU_data = $DBH->selectall_hashref("select
> ram,ip,application,hosting,assigned_to,rdom0id from domU_info where
> server_name='$domU_server' ",'server_name' );
>
> my $select_all_website = $DBH->selectall_hashref("select
> website_id,website_name from websites_name ",'website_id');
>
> now i need to push $select_all_website into $selet_domU_data
>
> my %hash1 = %$select_all_website;
>
> foreach (keys %$selet_domU_data) {
> push (@{$selet_domU_data->{$_}}, { rets => %hash1 );
> }
>
>
> print Dumper ([$selet_domU_data]);
>
> i also tried a combination of many other things but does not seem to work
>
> thanks in advanced
>
>
> --
> Regards
> Agnello D'souza
>
--
Marcos Rebelo
http://www.oleber.com/
Milan Perl Mongers leader https://sites.google.com/site/milanperlmongers/
Webmaster of http://perl5notebook.oleber.com
--0023543a2b92e8d1d104a51a610f--
Re: how do i push a hashref into another hashref
am 07.06.2011 10:04:58 von Agnello George
--20cf305b12b619877f04a51aadda
Content-Type: text/plain; charset=ISO-8859-1
On Tue, Jun 7, 2011 at 1:13 PM, marcos rebelo wrote:
> I don't understand what you are trying to do
>
>
>
Ok , i got 2 hashes
$hashref1 = [ { 'windows2 ' => { 'lvm' => ' windows2 8GB ram 50 GB HDD',
'cpu' => 'na', 'ip' => ' 171.16.1.10', 'application' => ' win2k8 ,sql,iis',
'hosting' => '', 'assigned_to' => ali', 'rdom0id' => '11', 'server_name' =>
'windows2 ', 'ram' => ' na' } } ];
$hashref2 = [ { '6' => { 'website_name' => 'website.com', 'website_id' =>
'6' }, '3' => { 'website_name' => 'life.com', 'website_id' => '3' }, '7' =>
{ 'website_name' => 'writes.com', 'website_id' => '7' } } ];
i need to push $hashref2 into hashref1
need to get some thing like
$hashref1 = [ { 'windows2 ' => { 'lvm' => ' windows2 8GB ram 50 GB HDD',
'cpu' => 'na', 'ip' => ' 171.16.1.10', 'application' => ' win2k8 ,sql,iis',
'hosting' => '', 'assigned_to' => ali', 'rdom0id' => '11', 'server_name' =>
'windows2 ', 'ram' => ' na' }, all_websites => { '6' => { 'website_name' =>
'website.com', 'website_id' => '6' }, '3' => { 'website_name' => 'life.com',
'website_id' => '3' }, '7' => { 'website_name' => 'writes.com', 'website_id'
=> '7' } } } ];
this looks a bit confusing but i need to get it this way as i need to pass
it through a template .
--
Regards
Agnello D'souza
--20cf305b12b619877f04a51aadda--
Re: how do i push a hashref into another hashref
am 07.06.2011 10:10:28 von Uri Guttman
>>>>> "AG" == Agnello George writes:
AG> i need to push $hashref2 into hashref1
that is the major flaw in your logic. you can't push into hashes. you
can assign a hash ref to a slot in a hash. please use the correct terms
as it will make it easier for you to be understood.
AG> need to get some thing like
AG> $hashref1 = [ { 'windows2 ' => { 'lvm' => ' windows2 8GB ram 50 GB HDD',
AG> 'cpu' => 'na', 'ip' => ' 171.16.1.10', 'application' => ' win2k8 ,sql,iis',
AG> 'hosting' => '', 'assigned_to' => ali', 'rdom0id' => '11', 'server_name' =>
AG> 'windows2 ', 'ram' => ' na' }, all_websites => { '6' => { 'website_name' =>
AG> 'website.com', 'website_id' => '6' }, '3' => { 'website_name' => 'life.com',
AG> 'website_id' => '3' }, '7' => { 'website_name' => 'writes.com', 'website_id'
AG> => '7' } } } ];
that is unreadable. print them with Data::Dumper so we can see the
layout of you data tree.
AG> this looks a bit confusing but i need to get it this way as i need to pass
AG> it through a template .
it is only confusing because you printed it in a confusing
style. remember we have to read what you post here, not read your
mind. so post well formatted data and code to make it easier on our eyes
and brains.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how do i push a hashref into another hashref
am 07.06.2011 10:12:36 von Rob Coops
--0015175cb9f68ee06e04a51ac9d8
Content-Type: text/plain; charset=UTF-8
On Tue, Jun 7, 2011 at 8:47 AM, Agnello George wrote:
> HI
>
> I got the following hashref
>
> my $selet_domU_data = $DBH->selectall_hashref("select
> ram,ip,application,hosting,assigned_to,rdom0id from domU_info where
> server_name='$domU_server' ",'server_name' );
>
> my $select_all_website = $DBH->selectall_hashref("select
> website_id,website_name from websites_name ",'website_id');
>
> now i need to push $select_all_website into $selet_domU_data
>
> my %hash1 = %$select_all_website;
>
> foreach (keys %$selet_domU_data) {
> push (@{$selet_domU_data->{$_}}, { rets => %hash1 );
> }
>
>
> print Dumper ([$selet_domU_data]);
>
> i also tried a combination of many other things but does not seem to work
>
> thanks in advanced
>
>
> --
> Regards
> Agnello D'souza
>
Hi Angello,
Could you maybe draw what you want the result to look like?
You are saying you have a hashref called: $selet_domU_data and one
called: $select_all_website right. Now both of them seem to be the result of
a database handle executing the query that you showed behind them. So both
of them will contain a hashref with in there a key (server_name, website_id
respectively) and all values fetched for these keys.
So eacy of them will look like this:
{
HASH1_Key1 => { Column1 => '...', Column2 => '...', }
HASH1_Key2 => { Column1 => '...', Column2 => '...', }
...
}
and
{
HASH2_Key1 => { Column1 => '...', Column2 => '...', }
HASH2_Key2 => { Column1 => '...', Column2 => '...', }
...
}
Combining them will end up with:
{
HASH1_Key1 => { Column1 => '...', Column2 => '...', }
HASH1_Key2 => { Column1 => '...', Column2 => '...', }
...
HASH2_Key1 => { Column1 => '...', Column2 => '...', }
HASH2_Key2 => { Column1 => '...', Column2 => '...', }
...
}
right?
Well then it should be simple enough:
foreach my $key ( keys %{ $hashref_a } ) {
${ $hashref_b }{ $key } = ${ $hashref_a }{ $key };
}
Basically loop over one of the two hashs and shove all it's key value pairs
into the other and you are done. If you don't need the initial hashref
anymore don't forget to clear it so perl will not keep the hash in memory
for any longer then you absolutely need to. (after all you never know how
big that database might be)
Regards,
Rob
--0015175cb9f68ee06e04a51ac9d8--
Re: how do i push a hashref into another hashref
am 07.06.2011 10:24:07 von Agnello George
--20cf305b12b694b73e04a51af1fb
Content-Type: text/plain; charset=ISO-8859-1
On Tue, Jun 7, 2011 at 1:42 PM, Rob Coops wrote:
>
>
> On Tue, Jun 7, 2011 at 8:47 AM, Agnello George wrote:
>
>> HI
>>
>> I got the following hashref
>>
>> my $selet_domU_data = $DBH->selectall_hashref("select
>> ram,ip,application,hosting,assigned_to,rdom0id from domU_info where
>> server_name='$domU_server' ",'server_name' );
>>
>> my $select_all_website = $DBH->selectall_hashref("select
>> website_id,website_name from websites_name ",'website_id');
>>
>> now i need to push $select_all_website into $selet_domU_data
>>
>> my %hash1 = %$select_all_website;
>>
>> foreach (keys %$selet_domU_data) {
>> push (@{$selet_domU_data->{$_}}, { rets => %hash1 );
>> }
>>
>>
>> print Dumper ([$selet_domU_data]);
>>
>> i also tried a combination of many other things but does not seem to work
>>
>> thanks in advanced
>>
>>
>> --
>> Regards
>> Agnello D'souza
>>
>
>
> Hi Angello,
>
> Could you maybe draw what you want the result to look like?
>
> You are saying you have a hashref called: $selet_domU_data and one
> called: $select_all_website right. Now both of them seem to be the result of
> a database handle executing the query that you showed behind them. So both
> of them will contain a hashref with in there a key (server_name, website_id
> respectively) and all values fetched for these keys.
>
> So eacy of them will look like this:
> {
> HASH1_Key1 => { Column1 => '...', Column2 => '...', }
> HASH1_Key2 => { Column1 => '...', Column2 => '...', }
> ...
> }
> and
> {
> HASH2_Key1 => { Column1 => '...', Column2 => '...', }
> HASH2_Key2 => { Column1 => '...', Column2 => '...', }
> ...
> }
>
> Combining them will end up with:
> {
> HASH1_Key1 => { Column1 => '...', Column2 => '...', }
> HASH1_Key2 => { Column1 => '...', Column2 => '...', }
> ...
> HASH2_Key1 => { Column1 => '...', Column2 => '...', }
> HASH2_Key2 => { Column1 => '...', Column2 => '...', }
> ...
> }
>
> right?
>
> Well then it should be simple enough:
> foreach my $key ( keys %{ $hashref_a } ) {
> ${ $hashref_b }{ $key } = ${ $hashref_a }{ $key };
> }
>
> Basically loop over one of the two hashs and shove all it's key value pairs
> into the other and you are done. If you don't need the initial hashref
> anymore don't forget to clear it so perl will not keep the hash in memory
> for any longer then you absolutely need to. (after all you never know how
> big that database might be)
>
> Regards,
>
> Rob
>
Ok i think was was able to get this done using the following code
$selet_domU_data = [ { 'windows2 ' => { 'lvm' => ' windows2 8GB ram 50 GB
HDD',
'cpu' => 'na',
'ip' => '
171.16.1.10',
'application' => ' win2k8
,sql,iis',
'hosting' => '',
'assigned_to' => ali',
rdom0id' => '11',
'server_name' => 'windows2 ',
'ram' => ' na' } } ];
$select_all_website = [ { '6' => { 'website_name' => 'website.com',
'website_id' => '6' },
'3' => { 'website_name' => 'life.com',
'website_id' => '3' },
'7' => { 'website_name' => 'writes.com',
'website_id' => '7' } } ];
foreach my $ke1 (keys %$selet_domU_data) {
foreach my $ke2 (keys %$select_all_website ) {
push (@{$selet_domU_data->{$ke1}{'all_web'}}, {$ke2 =>
$select_all_website->{$ke2}} );
}
}
print Dumper ([$selet_domU_data]);
$selet_domU_data = [ { 'windows2 ' => { 'lvm' => ' windows2 8GB ram 50 GB
HDD',
'cpu' => 'na',
'ip' => ' 171.16.1.10',
'application' => ' win2k8
,sql,iis',
'hosting' => '',
'assigned_to' => ali',
'rdom0id' => '11',
'server_name' => 'windows2 ',
'ram' => ' na' },
all_websites => { '6' => { 'website_name' =>
'website.com', 'website_id' => '6' },
'3' => {
'website_name' => 'life.com', 'website_id' => '3' },
'7' => {
'website_name' => 'writes.com', 'website_id' => '7' } } } ];
this is what i wanted , isn't this called pushing a hash into another hash .
Thanks for all the help
--
Regards
Agnello D'souza
--20cf305b12b694b73e04a51af1fb--
Re: how do i push a hashref into another hashref
am 07.06.2011 10:35:26 von Uri Guttman
>>>>> "AG" == Agnello George writes:
AG> $selet_domU_data = [ { 'windows2 ' => { 'lvm' => ' windows2 8GB ram 50 GB
AG> HDD',
AG> 'cpu' => 'na',
AG> 'ip' => '
AG> 171.16.1.10',
AG> 'application' => ' win2k8
AG> ,sql,iis',
AG> 'hosting' => '',
AG> 'assigned_to' => ali',
AG> rdom0id' => '11',
AG> 'server_name' => 'windows2 ',
AG> 'ram' => ' na' } } ];
what are you doing to make that so unreadable? seriously. you claim to
be calling Data::Dumper but the formatting is all over the place. that
is unreadable so we can't check out what you want or have.
AG> $select_all_website = [ { '6' => { 'website_name' => 'website.com',
AG> 'website_id' => '6' },
AG> '3' => { 'website_name' => 'life.com',
AG> 'website_id' => '3' },
AG> '7' => { 'website_name' => 'writes.com',
AG> 'website_id' => '7' } } ];
maybe you have set the format value of dumper to something else? or your
emailer is destroying the format with line wrapping abuse? but please
learn how to show clean data trees here (and elsewhere). it will make
your understand of the data infinitely easier.
AG> foreach my $ke2 (keys %$select_all_website ) {
AG> push (@{$selet_domU_data->{$ke1}{'all_web'}}, {$ke2 =>
AG> $select_all_website->{$ke2}} );
AG> }
AG> }
reformatted for humans to read. white space added, proper indenting,
newlines where needed.
foreach my $ke2 ( keys %{$select_all_website} ) {
push( @{ $selet_domU_data->{$ke1}{'all_web'} },
{
$ke2 => AG> $select_all_website->{$ke2}
}
) ;
}
AG> this is what i wanted , isn't this called pushing a hash into
AG> another hash .
as i said in another post, you can't push onto a hash. you can push into
an array reference that is IN the hash. with the reformatted code you
can see the push (which ONLY take an array for its first arg) is getting
a hash ref for its data. the parent hash has an array ref there (or will
after the push due to autovivification).
but please stop calling that a push into a hash as it isn't one.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how do i push a hashref into another hashref
am 07.06.2011 10:50:49 von Shlomi Fish
Hi Agnello,
On Tuesday 07 Jun 2011 11:04:58 Agnello George wrote:
> On Tue, Jun 7, 2011 at 1:13 PM, marcos rebelo wrote:
> > I don't understand what you are trying to do
>=20
> Ok , i got 2 hashes
>=20
> $hashref1 =3D [ { 'windows2 ' =3D> { 'lvm' =3D> ' windows2 8GB ram 50 G=
B HDD',
> 'cpu' =3D> 'na', 'ip' =3D> ' 171.16.1.10', 'application' =3D> ' win2k8 ,s=
ql,iis',
> 'hosting' =3D> '', 'assigned_to' =3D> ali', 'rdom0id' =3D> '11', 'server_=
name' =3D>
> 'windows2 ', 'ram' =3D> ' na' } } ];
>=20
> $hashref2 =3D [ { '6' =3D> { 'website_name' =3D> 'website.com', 'website_=
id' =3D>
> '6' }, '3' =3D> { 'website_name' =3D> 'life.com', 'website_id' =3D> '3' }=
, '7' =3D>
> { 'website_name' =3D> 'writes.com', 'website_id' =3D> '7' } } ];
>=20
>=20
>=20
> i need to push $hashref2 into hashref1
>=20
>=20
> need to get some thing like
>=20
> $hashref1 =3D [ { 'windows2 ' =3D> { 'lvm' =3D> ' windows2 8GB ram 50 G=
B HDD',
> 'cpu' =3D> 'na', 'ip' =3D> ' 171.16.1.10', 'application' =3D> ' win2k8 ,s=
ql,iis',
> 'hosting' =3D> '', 'assigned_to' =3D> ali', 'rdom0id' =3D> '11', 'server_=
name' =3D>
> 'windows2 ', 'ram' =3D> ' na' }, all_websites =3D> { '6' =3D> { 'website=
_name'
> =3D> 'website.com', 'website_id' =3D> '6' }, '3' =3D> { 'website_name' =
=3D>
> 'life.com', 'website_id' =3D> '3' }, '7' =3D> { 'website_name' =3D>
> 'writes.com', 'website_id' =3D> '7' } } } ];
You don't push a hashref into another hashref. And $hashref1 is clearly an=
=20
array reference (which makes its name confusing). Please go over the releva=
nt=20
resources about references and nested data structures in Perl here:
http://perl-begin.org/topics/references/
After that you can use "perl -d" to do things interactively and debug your=
=20
program:
http://perl-begin.org/topics/debugging/
Unit tests may also be a good idea:
http://perl-begin.org/uses/qa/
Regards,
Shlomi Fish
=2D-=20
=2D--------------------------------------------------------- -------
Shlomi Fish http://www.shlomifish.org/
Interview with Ben Collins-Sussman - http://shlom.in/sussman
I promised, I forgot, I did not keep my promise â=94 just shoot me, an=
d get on
with it!
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how do i push a hashref into another hashref
am 07.06.2011 13:30:31 von Leo Lapworth
Hi Agnello,
On 7 June 2011 07:47, Agnello George wrote:
> I got the following hashref
> now i need to push $select_all_website into $selet_domU_data
I think this is what you want...
use strict;
use warnings;
my $hash_ref_1 = {
a => 1,
b => 2,
};
my $hash_ref_2 = {
a => 3,
c => 4,
};
my $new_hash_ref = {
%{$hash_ref_1},
%{$hash_ref_2}
};
use Data::Dumper;
print Dumper($new_hash_ref);
Which produces:
$VAR1 = {
'c' => 4,
'a' => 3,
'b' => 2
};
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how do i push a hashref into another hashref
am 07.06.2011 18:48:32 von byulga
Leo Lapworth wrote:
> Hi Agnello,
>
> On 7 June 2011 07:47, Agnello George wrote:
>
>> I got the following hashref
>>
>
>
>> now i need to push $select_all_website into $selet_domU_data
>>
> I think this is what you want...
>
> use strict;
> use warnings;
>
> my $hash_ref_1 = {
> a => 1,
> b => 2,
> };
>
> my $hash_ref_2 = {
> a => 3,
> c => 4,
> };
>
> my $new_hash_ref = {
> %{$hash_ref_1},
> %{$hash_ref_2}
> };
>
> use Data::Dumper;
> print Dumper($new_hash_ref);
>
> Which produces:
>
> $VAR1 = {
> 'c' => 4,
> 'a' => 3,
> 'b' => 2
> };
>
>
Hi Leo,
You are probably close to what the OP wanted; it is hard to read minds
since "pushing onto hashes" makes no sense. I would call it "merging
two hashes". For the OP's data maybe this isn't an issue (and hence why
you didn't state it), but in your sample data, the value for key 'a' in
hash_ref_1 is overwritten by the value in hash_ref_2 during the merge.
Even though your way is short and concise, perhaps it's safer to check
each key before assignment? I suppose the decision to check depends on
the predictability of the source data...
my $combined_hash = { %$hash_ref_1 };
foreach my $key_in_hsh2 ( keys %$hash_ref_2 ) {
if (exists $hash_ref_1->{$key_in_hsh2} ) {
# some algorithm to deal with duplicate keys or alert user
warn "found duplicate key '$key_in_hsh2', keeping original
value\n";
} else {
$combined_hash->{$key_in_hsh2} = $hash_ref_2->{$key_in_hsh2}
}
}
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how do i push a hashref into another hashref
am 07.06.2011 19:14:07 von Shawn H Corey
On 11-06-07 12:48 PM, Brian F. Yulga wrote:
> Even though your way is short and concise, perhaps it's safer to check
> each key before assignment?
Or you could make the new hash a hash of arrays:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;
# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;
my $hash_ref_1 = {
a => 1,
b => 2,
};
my $hash_ref_2 = {
a => 3,
c => 4,
};
my $new_hash_ref = {};
load_hash( $new_hash_ref, $hash_ref_1 );
load_hash( $new_hash_ref, $hash_ref_2 );
print Dumper $new_hash_ref;
# --------------------------------------
# Name: load_hash
# Usage: load_hash( \%new_hash_ref, $hash_ref );
# Purpose: push the values of the second hash onto the first
# Parameters: \%new_hash_ref -- where they go
# $hash_ref -- where they're from
# Returns: (none)
#
sub load_hash {
my $new_hash_ref = shift @_;
my $hash_ref = shift @_;
for my $key ( keys %$hash_ref ){
push @{ $new_hash_ref->{$key} }, $hash_ref->{$key};
}
return;
}
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how do i push a hashref into another hashref
am 07.06.2011 23:30:54 von Leo Lapworth
On 7 June 2011 17:48, Brian F. Yulga wrote:
> Leo Lapworth wrote:
>> On 7 June 2011 07:47, Agnello George =A0wrote:
>>>
>>> I got the following hashref
>>>
>>
>>
>>
>>>
>>> now i need to push $select_all_website into $selet_domU_data
>>>
>>
>> I think this is what you want...
>>
>> use strict;
>> use warnings;
>>
>> my $hash_ref_1 =3D {
>> =A0 =A0 =A0 =A0 a =3D> =A01,
>> =A0 =A0 =A0 =A0 b =3D> =A02,
>> };
>>
>> my $hash_ref_2 =3D {
>> =A0 =A0 =A0 =A0 a =3D> =A03,
>> =A0 =A0 =A0 =A0 c =3D> =A04,
>> };
>>
>> my $new_hash_ref =3D {
>> =A0 =A0 =A0 =A0 %{$hash_ref_1},
>> =A0 =A0 =A0 =A0 %{$hash_ref_2}
>> };
>>
>> use Data::Dumper;
>> print Dumper($new_hash_ref);
>>
>> Which produces:
>>
>> $VAR1 =3D {
>> =A0 =A0 =A0 =A0 =A0 'c' =3D> =A04,
>> =A0 =A0 =A0 =A0 =A0 'a' =3D> =A03,
>> =A0 =A0 =A0 =A0 =A0 'b' =3D> =A02
>> =A0 =A0 =A0 =A0 };
>>
>>
>
> You are probably close to what the OP wanted; it is hard to read minds si=
nce
> "pushing onto hashes" makes no sense. =A0I would call it "merging two has=
hes".
> =A0For the OP's data maybe this isn't an issue (and hence why you didn't =
state
> it), but in your sample data, the value for key 'a' in hash_ref_1 is
> overwritten by the value in hash_ref_2 during the merge. =A0Even though y=
our
> way is short and concise, perhaps it's safer to check each key before
> assignment? =A0I suppose the decision to check depends on the predictabil=
ity
> of the source data...
>
> my $combined_hash =3D { %$hash_ref_1 };
>
> foreach my $key_in_hsh2 ( keys %$hash_ref_2 ) {
>
> =A0 =A0if (exists $hash_ref_1->{$key_in_hsh2} ) {
> =A0 =A0 =A0 =A0# some algorithm to deal with duplicate keys or alert user
> =A0 =A0 =A0 =A0warn "found duplicate key '$key_in_hsh2', keeping original=
value\n";
>
> =A0 =A0} else {
> =A0 =A0 =A0 =A0$combined_hash->{$key_in_hsh2} =3D $hash_ref_2->{$key_in_h=
sh2}
> =A0 =A0}
> }
Hi Brian,
I should have pointed it out (it's why I gave that example), thanks
for the clarification.
Leo
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/