problem in creating a complex hash

problem in creating a complex hash

am 13.05.2011 12:11:25 von Agnello George

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 ( @$arrayref) {
push (@{$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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: problem in creating a complex hash

am 13.05.2011 13:00:57 von Rob Coops

--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 ( @{${$arrayref}[0]} ) {
if ( ! defined $hash{${$arrayreference}[0]} ) {
$hash{${$arrayreference}[0]} = { group => [ ${$arrayreference}[1] ] };
} else {
push @{${$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
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 ( @$arrayref) {
> push (@{$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@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

--0016e65dc8ec9965a804a3263938--

Re: problem in creating a complex hash

am 13.05.2011 13:27:56 von Agnello George

On Fri, May 13, 2011 at 4:30 PM, Rob Coops 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 ( @{${$arrayref}[0]} ) {
> =A0if ( ! defined $hash{${$arrayreference}[0]} ) {
> =A0 $hash{${$arrayreference}[0]} =3D { group =3D> [ ${$arrayreference}[1]=
] };
> =A0} else {
> =A0 push @{${$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



--=20
Regards
Agnello D'souza

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: problem in creating a complex hash

am 13.05.2011 13:46:50 von Paul Johnson

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 ( @{${$arrayref}[0]} ) {
> if ( ! defined $hash{${$arrayreference}[0]} ) {
> $hash{${$arrayreference}[0]} = { group => [ ${$arrayreference}[1] ] };
> } else {
> push @{${$hash{${$arrayreference}[0]}}{group}}, ${$arrayreference}[1];
> }
> }

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


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

print Dumper \%hash;

--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: problem in creating a complex hash

am 13.05.2011 14:15:33 von Rob Dixon

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 ( @$arrayref) {
> push (@{$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 (@{$arrayref->[0]}) {
my ($key, $val) = @$record;
push @{$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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: problem in creating a complex hash

am 13.05.2011 18:57:13 von Uri Guttman

>>>>> "RC" == Rob Coops 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 ( @{${$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 @{${$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@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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/