nchimato
nchimato

Reputation: 463

How can I associate multiple values with a single hash key?

I have a two-column file with the following format:

CLL     s_S616447
CLL     s_S612703
MBL     s_S599565
MBL     s_S577819
UnAff   s_S509781
UnAff   s_S754982

I want to make a hash with three keys, "CLL", "MBL", and "UnAff". With the code below I can assign the first column as the key and the second column as the value, but I don't know how to group the keys, per se, so that the hash only has those three keys rather than a key for every row in the file.

This is all I have so far:

open REF, "list.txt";

my %sam_type = ();

while (<REF>) {
    chomp $_;
    @cols = split("\t", $_);
    my $type = $cols[0];
    my $sample = $cols[1];
    $sam_type{$type} = $sample;
}

Can someone lead me in the right direction?

Upvotes: 1

Views: 844

Answers (2)

Henrik
Henrik

Reputation: 342

An alternative to using a hash of array refs is to use an ordinary hash of string, simply putting a comma or something inbetween the values:

$sam_type{$type} .= ",$sample";

When you need to you can split a string into an array.

Upvotes: -1

Matt Jacob
Matt Jacob

Reputation: 6553

You want a hash of arrays (HoA):

use strict;
use warnings;
use Data::Dump;

my %hash;

while (<DATA>) {
    my @fields = split;
    push(@{$hash{$fields[0]}}, $fields[1]);
}

dd(\%hash);

__DATA__
CLL     s_S616447
CLL     s_S612703
MBL     s_S599565
MBL     s_S577819
UnAff   s_S509781
UnAff   s_S754982

Output:

{
  CLL   => ["s_S616447", "s_S612703"],
  MBL   => ["s_S599565", "s_S577819"],
  UnAff => ["s_S509781", "s_S754982"],
}

Upvotes: 5

Related Questions