Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^5: Hash of Hashes from file

by scorpio17 (Abbot)
on Apr 03, 2012 at 15:19 UTC ( #963264=note: print w/replies, xml ) Need Help??

in reply to Re^4: Hash of Hashes from file
in thread Hash of Hashes from file

Yes, that should work. Then to add a new website/type, you could do this:
push( @{ $hoh{$user}{'Website'} }, 'website4'); push( @{ $hoh{$user}{'type'} }, 'type4'};

and to get all websites for a given user:

my @websites = @{ $hoh{$user}{'Website'} };

The syntax looks strange because you're storing an array ref, and have to dereference it.

Replies are listed 'Best First'.
Re^6: Hash of Hashes from file
by cipher (Acolyte) on Apr 03, 2012 at 16:01 UTC
    Thanks a lot,

    Can you also let me know how do I print all websites and types for each user ?

      If your data looks like this:

      %hoh = ( user1 => { Website => ['website1', 'website2', 'website3'], type => ['type1','type2','type3'], }, user2 => { Website => ['website1', 'website2', 'website3'], type => ['type1','type2','type3'], }, user3 => { Website => ['website1', 'website2', 'website3'], type => ['type1','type2','type3'], }, );
      Then try something like this (untested):
      for my $user (sort keys %hoh) { my @websites = @{ $hoh{$user}{'Website'} }; my @types = @{ $hoh{$user}{'type'} }; # assume we have the same number of each? unless (scalar(@websites) == scalar(@types)) { die "number of websites is different from number of types!"; } print "$user :\n"; for ( my $i=0; $i < scalar(@websites); ++$i) { print " $websites[$i]\n"; print " $types[$i]\n"; } print "\n"; }

      However, if you go with data like this:

      %hoh = ( # actually now a hash of arrays of hashes (HoAoH) user1 => [ { Website => 'website1', type => 'type1',}, { Website => 'website2', type => 'type2',}, { Website => 'website3', type => 'type3',}, ], user2 => [ { Website => 'website1', type => 'type1',}, { Website => 'website2', type => 'type2',}, { Website => 'website3', type => 'type3',}, ], user3 => [ { Website => 'website1', type => 'type1',}, { Website => 'website2', type => 'type2',}, { Website => 'website3', type => 'type3',}, ], );
      Then your code becomes (untested):
      for my $user (sort keys %hoh) { print "$user :\n"; # each element of this array is a hash ref for my $data ( @{ %hoh{$user} } ) { print " $data->{'Website'}\n"; print " $data->{'type'}\n"; } print "\n"; }

      So, depending on what you need to do, pick the data structure that makes your life easier.

        Logs are per line, My hash should look like the second example you have given above. I am getting syntax error for second code.

        I tried the first code given above and it works fine. I am able to get the all websites and categories for each user. Thanks a lot.

        I will now work on getting count for each Website and Type.
        Name: John Website Count 10 8 5 Type Count Search Engines 10 Entertainment 8 Social Networking 5

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://963264]
[erix]: "AngloZionist Empire"? "Stop the Empire's war on Russia?" WTF?
erix stops reading as recommended by the blog itself
[erix]: my attention being jealously guarded ;)
[shmem]: erix, yes, it's a WTF. Since...
[erix]: antisemitic && (pro-)russian. Makes sense :P
[shmem]: erix: what's antisemitic in there? Tell me.
[erix]: AngloZionist
[shmem]: and it is *not* pro-russian. It describes the western world.
[erix]: I'm not going to read it so I could be mistaken but it reeks of useful idiocy
[shmem]: AngloZionist is not an antisemitic term.

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2018-03-19 20:53 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (246 votes). Check out past polls.