problem in creating a complex hash

Hi All

I have a small issue in arranging data with a array ref .

$arrayref = [ [ [ 'user1, 'c'], [ 'user2', 'a'], [ 'user2', 'b' ],[
'user2', 'd' ],[ 'user3', 'a' ],[ 'user2', 'f' ] ] ];


i tried the following

my %sh ;

foreach my $i ( [at] $arrayref) {
push ( [at] {$sh{$i->[0]}},{group => [$i->[1] } );
}


required hash

%sh = ( user1 => { group => [ c ] },

user2 => { group => [ a b d f] },

user3 => { group => [ a ] }
)



but i am not able to get it in this format .

Can some one please help me out

Thanks a lot


--
Regards
Agnello D'souza

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Agnello George [ Fr, 13 Mai 2011 12:11 ] [ ID #2059479 ]

Re: problem in creating a complex hash

--0016e65dc8ec9965a804a3263938
Content-Type: text/plain; charset=UTF-8

It might not look nice but I would do the following:

#!/usr/local/bin/perl

use strict;
use warnings;

my $arrayref = [ [ [ 'user1', 'c'], [ 'user2', 'a'], [ 'user2', 'b' ],[
'user2', 'd' ],[ 'user3', 'a' ],[ 'user2', 'f' ] ] ];

my %hash;
foreach my $arrayreference ( [at] {${$arrayref}[0]} ) {
if ( ! defined $hash{${$arrayreference}[0]} ) {
$hash{${$arrayreference}[0]} = { group => [ ${$arrayreference}[1] ] };
} else {
push [at] {${$hash{${$arrayreference}[0]}}{group}}, ${$arrayreference}[1];
}
}

use Data::Dumper;
print Dumper %hash;

It prints:
$ perl test.pl
$VAR1 = 'user1';
$VAR2 = {
'group' => [
'c'
]
};
$VAR3 = 'user3';
$VAR4 = {
'group' => [
'a'
]
};
$VAR5 = 'user2';
$VAR6 = {
'group' => [
'a',
'b',
'd',
'f'
]
};

Which is I believe what you are after right?

Regards,

Rob

On Fri, May 13, 2011 at 12:11 PM, Agnello George
<agnello.dsouza [at] gmail.com>wrote:

> Hi All
>
> I have a small issue in arranging data with a array ref .
>
> $arrayref = [ [ [ 'user1, 'c'], [ 'user2', 'a'], [ 'user2', 'b' ],[
> 'user2', 'd' ],[ 'user3', 'a' ],[ 'user2', 'f' ] ] ];
>
>
> i tried the following
>
> my %sh ;
>
> foreach my $i ( [at] $arrayref) {
> push ( [at] {$sh{$i->[0]}},{group => [$i->[1] } );
> }
>
>
> required hash
>
> %sh = ( user1 => { group => [ c ] },
>
> user2 => { group => [ a b d f] },
>
> user3 => { group => [ a ] }
> )
>
>
>
> but i am not able to get it in this format .
>
> Can some one please help me out
>
> Thanks a lot
>
>
> --
> Regards
> Agnello D'souza
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>

--0016e65dc8ec9965a804a3263938--
Rob Coops [ Fr, 13 Mai 2011 13:00 ] [ ID #2059480 ]

Re: problem in creating a complex hash

On Fri, May 13, 2011 at 4:30 PM, Rob Coops <rcoops [at] gmail.com> wrote:
> It might not look nice but I would do the following:
> #!/usr/local/bin/perl
> use strict;
> use warnings;
> my $arrayref =3D [ [ [ 'user1', 'c'], [ 'user2', 'a'], [ 'user2', 'b' ],[
> 'user2', 'd' ],[ 'user3', 'a' ],[ 'user2', 'f' ] ] ];
> my %hash;
> foreach my $arrayreference ( [at] {${$arrayref}[0]} ) {
> =A0if ( ! defined $hash{${$arrayreference}[0]} ) {
> =A0 $hash{${$arrayreference}[0]} =3D { group =3D> [ ${$arrayreference}[1]=
] };
> =A0} else {
> =A0 push [at] {${$hash{${$arrayreference}[0]}}{group}}, ${$arrayreference}[1]=
;
> =A0}
> }
> use Data::Dumper;
> print Dumper %hash;
> It prints:
> $ perl test.pl
> $VAR1 =3D 'user1';
> $VAR2 =3D {
> =A0 =A0 =A0 =A0 =A0 'group' =3D> [
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0'c'
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0]
> =A0 =A0 =A0 =A0 };
> $VAR3 =3D 'user3';
> $VAR4 =3D {
> =A0 =A0 =A0 =A0 =A0 'group' =3D> [
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0'a'
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0]
> =A0 =A0 =A0 =A0 };
> $VAR5 =3D 'user2';
> $VAR6 =3D {
> =A0 =A0 =A0 =A0 =A0 'group' =3D> [
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0'a',
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0'b',
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0'd',
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0'f'
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0]
> =A0 =A0 =A0 =A0 };
> Which is I believe what you are after right?

thanks a lot this seem to have worked



--
Regards
Agnello D'souza

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Agnello George [ Fr, 13 Mai 2011 13:27 ] [ ID #2059481 ]

Re: problem in creating a complex hash

On Fri, May 13, 2011 at 01:00:57PM +0200, Rob Coops wrote:
> It might not look nice but I would do the following:

But it can be cleaned up quite a lot:

> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> my $arrayref = [ [ [ 'user1', 'c'], [ 'user2', 'a'], [ 'user2', 'b' ],[
> 'user2', 'd' ],[ 'user3', 'a' ],[ 'user2', 'f' ] ] ];
>
> my %hash;
> foreach my $arrayreference ( [at] {${$arrayref}[0]} ) {
> if ( ! defined $hash{${$arrayreference}[0]} ) {
> $hash{${$arrayreference}[0]} = { group => [ ${$arrayreference}[1] ] };
> } else {
> push [at] {${$hash{${$arrayreference}[0]}}{group}}, ${$arrayreference}[1];
> }
> }

foreach my $arrayreference ( [at] {$arrayref->[0]} ) {
push [at] {$hash{$arrayreference->[0]}->{group}}, $arrayreference->[1];
}


> use Data::Dumper;
> print Dumper %hash;

print Dumper \%hash;

--
Paul Johnson - paul [at] pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Paul Johnson [ Fr, 13 Mai 2011 13:46 ] [ ID #2059482 ]

Re: problem in creating a complex hash

On 13/05/2011 11:11, Agnello George wrote:
> Hi All
>
> I have a small issue in arranging data with a array ref .
>
> $arrayref = [ [ [ 'user1, 'c'], [ 'user2', 'a'], [ 'user2', 'b' ],[
> 'user2', 'd' ],[ 'user3', 'a' ],[ 'user2', 'f' ] ] ];
>
>
> i tried the following
>
> my %sh ;
>
> foreach my $i ( [at] $arrayref) {
> push ( [at] {$sh{$i->[0]}},{group => [$i->[1] } );
> }
>
>
> required hash
>
> %sh = ( user1 => { group => [ c ] },
>
> user2 => { group => [ a b d f] },
>
> user3 => { group => [ a ] }
> )
>
>
>
> but i am not able to get it in this format .
>
> Can some one please help me out

Hello

Something like the program below perhaps?

HTH,

Rob


use strict;
use warnings;

use Data::Dumper;
$Data::Dumper::Sortkeys = 1;

my $arrayref = [ [
[ 'user1', 'c' ],
[ 'user2', 'a' ],
[ 'user2', 'b' ],
[ 'user2', 'd' ],
[ 'user3', 'a' ],
[ 'user2', 'f' ]
] ];

my %sh;

foreach my $record ( [at] {$arrayref->[0]}) {
my ($key, $val) = [at] $record;
push [at] {$sh{$key}{group}}, $val;
}

print Data::Dumper->Dump([\%sh], ['*sh']);

**OUTPUT**

%sh = (
'user1' => {
'group' => [
'c'
]
},
'user2' => {
'group' => [
'a',
'b',
'd',
'f'
]
},
'user3' => {
'group' => [
'a'
]
}
);

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Fr, 13 Mai 2011 14:15 ] [ ID #2059483 ]

Re: problem in creating a complex hash

>>>>> "RC" == Rob Coops <rcoops [at] gmail.com> writes:

RC> my $arrayref = [ [ [ 'user1', 'c'], [ 'user2', 'a'], [ 'user2', 'b' ],[
RC> 'user2', 'd' ],[ 'user3', 'a' ],[ 'user2', 'f' ] ] ];

this comment is for both you and the OP. you should format complex data
so you can read it. it makes it much easier to edit and modify it later
on. i can't tell which level those elements are in so it makes it harder
to code for it.


RC> my %hash;
RC> foreach my $arrayreference ( [at] {${$arrayref}[0]} ) {
RC> if ( ! defined $hash{${$arrayreference}[0]} ) {

why do you think you need to check for that?
RC> $hash{${$arrayreference}[0]} = { group => [ ${$arrayreference}[1] ] };

RC> } else {
RC> push [at] {${$hash{${$arrayreference}[0]}}{group}}, ${$arrayreference}[1];

if you just did that single line and not the if and then parts, it would
still work fine. this is called autovivification. it is one of those
very nice things perl does for you.

also please learn to bottom post and edit the quoted email.

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Fr, 13 Mai 2011 18:57 ] [ ID #2059490 ]
Perl » gmane.comp.lang.perl.beginners » problem in creating a complex hash

Vorheriges Thema: pushing a string into a array
Nächstes Thema: help me with a parsing script please