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: <CODE> #!/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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/