<p>Given the following file:</p>

<pre><code>department=value1
location=valueA
location=valueB
department=value2
</code></pre>

<p>I use the following to load the file into a Perl hash:</p>

<pre><code>use File::Slurp;
use Data::Dumper;
my %hash = map {
   s/#.*//;
   s/^\s+//;
   s/\s+$//;
   m/(.*?)\s*=\s*(.*)/;
} read_file($file);
print Dumper(\%hash);
</code></pre>

<p>The result, however, is as follows:</p>

<pre><code>$VAR1 = {
          'location' =&gt; 'valueB',
          'department' =&gt; 'value2'
        };
</code></pre>

<p>How can I load the above file into a hash with, say,</p>

<pre><code>$VAR1 = {
          'location' =&gt; 'valueA,valueB',
          'department' =&gt; 'value1,value2'
        };
</code></pre>

<p>Thanks.</p>

Here you go:
```
```
my %hash;
while (<FILE>)
{
   chomp;
   my ($key, $val) = split /=/;
   $hash{$key} .= exists $hash{$key} ? ",$val" : $val;
}
```
```
This walks through every line splitting on the '=' sign and either adds an entry or appends to an existing entry in the hash table.