On Tue, Jun 7, 2011 at 1:42 PM, Rob Coops <[email protected]> wrote:
>
>
> On Tue, Jun 7, 2011 at 8:47 AM, Agnello George
> <[email protected]>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