Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How to create multiple keys in a hash

by tanuja (Initiate)
on Sep 20, 2003 at 22:50 UTC ( #292920=perlquestion: print w/replies, xml ) Need Help??

tanuja has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to create a hash with multiple keys in it. eg. i want to create a hash on sunny,true, 1and count the number of occurences of such a hash. i want to retrive it by hashing on these multiple keys which i dont know how to do.

I create the ahsh in the following manner.

%hashlist =(); foreach key1,key2,key3 do $hashlist->{key1,key2,key3}++; and to list the occurences of such a hash i do, foreach $key(keys %hashlist) { ($dim1,$dim2,$dim3) = (split %;, $key)[0,1,2]; print "$hashlist->{dim0} "; }
and this print the occurences of such multiple keys.

now given a new entry (k1,k2,k3) i want to map it into the hash . how do i do it. i wnat tp know the frequency of k1,k2,k3 . how do i doit. if you can suggest me any other method. It will be of real help to me.

Thanks.
Tanuja.

edited: Sun Sep 21 00:01:46 2003 by jeffa - code tags, formatting

Replies are listed 'Best First'.
Re: How to create multiple keys in a hash
by thinker (Parson) on Sep 20, 2003 at 23:06 UTC

    Hi tanuja,

    If I understand correctly, you want to count the occurences of elements of a list. If so, this might help.

    #!/usr/bin/perl use strict; use warnings FATAL => 'all'; my @colours = ( "red", "green", "yellow", "green", "yellow", "green", + "blue", "red", "green"); my %count; $count{ $_ }++ for @colours; print "$_\t$count{ $_ }\n" for keys %count; __END__ output green 4 blue 1 red 2 yellow 2

    Hope this helps

    thinker

      Hello, That code will work sice the list is only one. I want to count the occurence of the combination of the lists. and also to know how to map them. i have 3 lists @list1 ={sunny,windy} @list2 = {t,f,b} @list3 = {a,b} and i have records sunny t a sunny t b sunny f a sunny f a sunny b b windy f a windy f b and so on , so i want to know te occurence of all combinations of these records. then i want to count the occurence of sunny,t,a and sunny,t,b and sunny,f,a and sunny,f,b, windy,t,a, ..... so the hash should have multiple keys i.e from list1,list2,list3. and i could craete this as i have described in my question. but i dont know how to retrive. i.e i have an entry "sunny,f,b " how can i know its frequency. Please kindly let me know. Thanks
      Hi, In my reply the records seem to be mangled.
      I will have records as follows
      (sunny,a,b)
      (sunny,f,b)
      (sunny,f,a)
      (windy,t,a)
      and so on. ... so what is the occurence of each such record which is a combination of keys from the 3 lists. Tanuja.
Re: How to create multiple keys in a hash
by artist (Parson) on Sep 20, 2003 at 23:58 UTC
    Hi Tanuja,

    See if this is what you want..

    my $records; while(<DATA>){ chomp; my @fields = split /\s+/,$_; $records->{$fields[0]}{$fields[1]}{$fields[2]} += 1; } foreach my $level1 (keys %$records){ foreach my $level2 (keys %{$records->{$level1}}){ foreach my $level3 (keys %{$records->{$level1}{$level2}} +){ print "$level1 $level2 $level3 => ", $records->{$level1}{$leve +l2}{$level3},"\n"; } } } __DATA__ sunny t a sunny t b sunny f a sunny f a sunny b b windy f a windy f b __OUTPUT__ windy f a => 1 windy f b => 1 sunny f a => 2 sunny b b => 1 sunny t a => 1 sunny t b => 1

    {artist}

      Hi artist, I have typed out this code in perl and tested if it works but it isnt resulting in any output. Please let me know. Thanks
Re: How to create multiple keys in a hash
by bl0rf (Pilgrim) on Sep 21, 2003 at 00:31 UTC
    I'm not exactly sure what you want to do with the
    modified hash implementation but you answered your
    own question by using split() in your example code
    :-)

    Simply use a separated string as your key and iterate
    over the values in the string as if they were keys:

    %modhash = (); $modhash{ 'first:second:third' } = [ 'bob', 'bill', 'bo' ]; $modhash{ 'Canada:USA:China' } = [ 'beaver','eagle','Mao' ]; # two sample entries, notice the anonymous arrays foreach $key ( keys %modhash ) { @subkeys = split( /:/, $key ); for( $i = 0; $i < scalar @subkeys; $i++ ) { print "subkey-> $subkeys[$i]\t", "subvalue-> $modhash{$key}->[$i]\n"; } print "\n"; # separates different main keys }
    This code is tested ( ActivePerl 5.6.0 ) and produces
    this output:

    subkey-> Canada subvalue-> beaver
    subkey-> USA subvalue-> eagle
    subkey-> China subvalue-> Mao

    subkey-> first subvalue-> bob
    subkey-> second subvalue-> bill
    subkey-> third subvalue-> bo

    I've got a feeling that you might accomplish your
    task in an easier manner.

    My Site

Re: How to create multiple keys in a hash
by jonadab (Parson) on Sep 21, 2003 at 00:16 UTC

    Will a nested hash structure do what you want?

    $somehash{foo}{bar}{baz}++; $somehash{quux}{wibble}{42}++; foreach $x (keys %somehash) { foreach $y (keys %{$somehash{$x}}) { foreach $z (keys %{$somehash{$x}{$y}}) { print "There were $somehash{$x}{$y}{$z} ". "occurrances of $x,$y,$z\n"; } } }

    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
      Hi Jonadab, I have tried this code but it doesnt work and doesnt give me any output. Please let me know. Thanks.
        I have tried this code but it doesnt work and doesnt give me any output.

        It works for me here. Here's what it does under Perl 5.6 on Linux:

        [family@pedestrian family]$ perl -e ' > $somehash{foo}{bar}{baz}++; > $somehash{quux}{wibble}{42}++; > > foreach $x (keys %somehash) { > foreach $y (keys %{$somehash{$x}}) { > foreach $z (keys %{$somehash{$x}{$y}}) { > print "There were $somehash{$x}{$y}{$z} ". > "occurrances of $x,$y,$z\n"; > } > } > } > > ' There were 1 occurrances of foo,bar,baz There were 1 occurrances of quux,wibble,42 [family@pedestrian family]$

        update: also works on ActiveState Perl (5.8) on WinMe:

        F:\perl>perl temp.pl There were 1 occurrances of quux,wibble,42 There were 1 occurrances of foo,bar,baz

        So if it doesn't work for you, please post a reply with a copy-and-paste showing what you tried and what error message you got.


        $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://292920]
Approved by allolex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2021-10-27 07:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (91 votes). Check out past polls.

    Notices?