On Tue, Jun 7, 2011 at 8:47 AM, Agnello George <agnello.dso...@gmail.com>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

Reply via email to