Loading ...
Sorry, an error occurred while loading the content.

composite key hash

Expand Messages
  • matt_johnsson
    All, I m currently using a composite key hash to gather counts of certain combinations of data. Bogus sample data: BOB,M,C TED,T,D BOB,M,C TED,D,D BOB,M,C so I
    Message 1 of 3 , Sep 2, 2003
    • 0 Attachment
      All,

      I'm currently using a composite key hash to gather counts
      of certain combinations of data.
      Bogus sample data:
      BOB,M,C
      TED,T,D
      BOB,M,C
      TED,D,D
      BOB,M,C
      so I go through my data and want to end up with:
      BOB,M,C 3
      TED,T,D 1
      TED,D,D 1
      I ended up using a composite key hash like
      if ( defined ( $myhash{$key1,$key2,$key3 }))
      { $myhash{$key1,$key2,$key3}++; }
      else
      { $myhash{$key1,$key2,$key3} = 1; }
      works fine, but when I want to print out the resulting hash, keys and
      values, how do I split out my composite keys?
      I keep getting all the keys concatinated like:
      BOBMC, which could be B OBM C or BO B MC or ....

      Any help / advice apreciated.

      Thanks
      /Matt
    • Jenda Krynicky
      From: matt_johnsson ... foreach my $item (keys %myhash) { my @subkeys = split $;, $item; print $subkeys[0], $subkeys[1],
      Message 2 of 3 , Sep 2, 2003
      • 0 Attachment
        From: "matt_johnsson" <matt_johnsson@...>
        > I'm currently using a composite key hash to gather counts
        > of certain combinations of data.
        > Bogus sample data:
        > BOB,M,C
        > TED,T,D
        > BOB,M,C
        > TED,D,D
        > BOB,M,C
        > so I go through my data and want to end up with:
        > BOB,M,C 3
        > TED,T,D 1
        > TED,D,D 1
        > I ended up using a composite key hash like
        > if ( defined ( $myhash{$key1,$key2,$key3 }))
        > { $myhash{$key1,$key2,$key3}++; }
        > else
        > { $myhash{$key1,$key2,$key3} = 1; }
        > works fine, but when I want to print out the resulting hash, keys and
        > values, how do I split out my composite keys? I keep getting all the
        > keys concatinated like: BOBMC, which could be B OBM C or BO B MC or

        foreach my $item (keys %myhash) {
        my @subkeys = split $;, $item;
        print "$subkeys[0], $subkeys[1], $subkeys[2]: $myhash{$item}\n"
        }

        See the description of $; in
        perldoc perlvar

        Jenda


        ===== Jenda@... === http://Jenda.Krynicky.cz =====
        When it comes to wine, women and song, wizards are allowed
        to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery
      • Damien Carbery
        ... Could you store stuff as: $myhash{ $key1,$key2,$key3 } i.e. put the key in a string. Then you can split the key when printing it out.
        Message 3 of 3 , Sep 2, 2003
        • 0 Attachment
          --- In perl-beginner@yahoogroups.com, "matt_johnsson"
          <matt_johnsson@y...> wrote:
          > All,
          >
          > I'm currently using a composite key hash to gather counts
          > of certain combinations of data.
          > Bogus sample data:
          > BOB,M,C
          > TED,T,D
          > BOB,M,C
          > TED,D,D
          > BOB,M,C
          > so I go through my data and want to end up with:
          > BOB,M,C 3
          > TED,T,D 1
          > TED,D,D 1
          > I ended up using a composite key hash like
          > if ( defined ( $myhash{$key1,$key2,$key3 }))
          > { $myhash{$key1,$key2,$key3}++; }
          > else
          > { $myhash{$key1,$key2,$key3} = 1; }
          > works fine, but when I want to print out the resulting hash, keys and
          > values, how do I split out my composite keys?
          > I keep getting all the keys concatinated like:
          > BOBMC, which could be B OBM C or BO B MC or ....
          >
          > Any help / advice apreciated.
          >
          > Thanks
          > /Matt

          Could you store stuff as:
          $myhash{"$key1,$key2,$key3"}
          i.e. put the key in a string.
          Then you can split the key when printing it out.
        Your message has been successfully submitted and would be delivered to recipients shortly.