title: How to append new values to hash in Perl
body: <p>I have a hash of hashes where, at the last level, I want each value to be appended - <strong>not updated</strong> - if that value already exists. What would be the best way to do this? I was thinking about making the values as lists, but this is probably not the most efficient way...</p>
  
  <p>Here's where I got so far:</p>
  
  <pre><code>#!/usr/local/bin/perl
  use strict;
  use warnings;
  use Data::Dumper;
  
  my $dir='D:\';
  
  open my $data,"&lt;","$dir\\file.txt";
  
  my (@selecteddata,@array,%hash);
  
  while (&lt;$data&gt;) {
      chomp $_;
      my @line= split "\t";
  
      $hash{$line[1]}{$line[2]}=$line[0]; 
      warn Dumper \%hash;
  }
  close $data;
  </code></pre>
  
  <p>Note, this code updates the values at last level with value <code>$line[0]</code>, but if the key <code>$line[4]</code> already exists (meaning, it already has a previous value <code>$line[0]</code>) I want this value to be appended and not updated.</p>
  
  <p>So, ideally, for the following (tab sepparated) list:</p>
  
  <pre><code>a1  b1  c1
  a2  b2  c2
  a3  b3  c3
  a4  b4  c4
  a5  b4  c4
  </code></pre>
  
  <p>The hash would look something like this - I don't know exactly how the grouping of <code>a4</code> and <code>a5</code> should look like, so as long as they are grouped it should be ok:</p>
  
  <pre><code>{
      'b1'    =&gt;  {'c1'   =&gt;  'a1'},
      'b2'    =&gt;  {'c2'   =&gt;  'a2'},
      'b3'    =&gt;  {'c3'   =&gt;  'a3'},
      'b4'    =&gt;  {'c4'   =&gt;  'a4, a5'}
  }
  </code></pre>