FAQ
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

Search Discussions

  • Shlomi Fish at Jun 4, 2011 at 11:58 am

    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 .

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJun 4, '11 at 11:44a
activeJun 4, '11 at 11:58a
posts2
users2
websiteperl.org

2 users in discussion

Agnello George: 1 post Shlomi Fish: 1 post

People

Translate

site design / logo © 2021 Grokbase