issue with a multidimensional hash .. unable recreate the desired hash
am 04.06.2011 13:44:11 von Agnello George
--000e0cd68c988f0a6b04a4e1639a
Content-Type: text/plain; charset=ISO-8859-1
hi
i have the following hash as out put from a sql query
VAR1 = [ { '16' => { 'srch_type_id' => '162', 'rid' => '2' }, '13' => {
'srch_type_id' => '123', 'rid' => '1' }, '17' => { 'srch_type_id' => '147',
'rid' => '2' }, '15' => { 'srch_type_id' => '135', 'rid' => '1' }, '14' => {
'srch_type_id' => '174', 'rid' => '1', } } ];
but i need to resort it with "rid" as the key , the out but as follow :
RID = [{ 1 => {
'13' => { 'srch_type_id' => '123', 'rid' => '1' },
'15' => { 'srch_type_id' => '135', 'rid' => '1' } ,
'14' => { 'srch_type_id' => '174', 'rid' => '1', }
},
2 => {
'16' => { 'srch_type_id' => '162', 'rid' => '2' },
'17' => { 'srch_type_id' => '147', 'rid' => '2' }
}
}]
but am unable to figure how to make value of VAR1 a key of RID
Can some one help me with this
--
Regards
Agnello D'souza
--000e0cd68c988f0a6b04a4e1639a--
Re: issue with a multidimensional hash .. unable recreate the desired hash
am 04.06.2011 13:58:36 von Shlomi Fish
On Saturday 04 Jun 2011 14:44:11 Agnello George wrote:
> hi
> i have the following hash as out put from a sql query
>
> VAR1 = [ { '16' => { 'srch_type_id' => '162', 'rid' => '2' }, '13' => {
> 'srch_type_id' => '123', 'rid' => '1' }, '17' => { 'srch_type_id' =>
> '147', 'rid' => '2' }, '15' => { 'srch_type_id' => '135', 'rid' => '1' },
> '14' => { 'srch_type_id' => '174', 'rid' => '1', } } ];
>
>
> but i need to resort it with "rid" as the key , the out but as follow :
>
> RID = [{ 1 => {
> '13' => { 'srch_type_id' => '123', 'rid' => '1' },
>
> '15' => { 'srch_type_id' => '135', 'rid' => '1' } ,
>
> '14' => { 'srch_type_id' => '174', 'rid' => '1', }
>
> },
>
> 2 => {
> '16' => { 'srch_type_id' => '162', 'rid' => '2' },
>
> '17' => { 'srch_type_id' => '147', 'rid' => '2' }
>
> }
> }]
>
>
> but am unable to figure how to make value of VAR1 a key of RID
>
> Can some one help me with this
Use a foreach loop:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $input = {
'16' => { 'srch_type_id' => '162', 'rid' => '2', },
'13' => { 'srch_type_id' => '123', 'rid' => '1', },
'17' => { 'srch_type_id' => '147', 'rid' => '2', },
'15' => { 'srch_type_id' => '135', 'rid' => '1', },
'14' => { 'srch_type_id' => '174', 'rid' => '1', } ,
};
my $output = {};
foreach my $key (keys(%$input))
{
my $rid = $input->{$key}->{rid};
$output->{$rid}->{$key} = +{ %{$input->{$key}} };
}
print Dumper($output);
CODE>
Regards,
Shlomi Fish
--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
"Star Trek: We, the Living Dead" - http://shlom.in/st-wtld
Live as if you were to die tomorrow. Learn as if you were to live forever.
-- http://en.wikiquote.org/wiki/Mohandas_Gandhi (Disputed)
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/