11

Given the following file:

department=value1
location=valueA
location=valueB
department=value2

I use the following to load the file into a Perl hash:

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

The result, however, is as follows:

$VAR1 = {
          'location' => 'valueB',
          'department' => 'value2'
        };

How can I load the above file into a hash with, say,

$VAR1 = {
          'location' => 'valueA,valueB',
          'department' => 'value1,value2'
        };

Thanks.

3 Answers 3

23

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.

2
  • Thanks. Just inserted chomp($val). Works like a charm. :)
    – doekman
    Oct 24, 2008 at 22:58
  • When I test on Windows with Perl 5.12.1, the values always have a preceding comma.... I have to change the last statement into a full if-conditional.
    – Yongwei Wu
    May 24, 2013 at 3:50
5

If you have control over the data file, consider switching from a custom format to something like YAML. This gives you a lot of power out of the box without having to hack your custom format more and more. In particular, multiple keys creating a list is non-obvious. YAML's way of doing it is much clearer.

name:       Wally Jones
department: [foo, bar]
location:   [baz, biff]

Note also that YAML allows you to sculpt the key/value pairs so they line up for easier reading.

And the code to parse it is done by a module, YAML::XS being the best of the bunch.

use File::Slurp;
use YAML::XS;
use Data::Dumper;

print Dumper Load scalar read_file(shift);

And the data structure looks like so:

$VAR1 = {
          'department' => [
                            'foo',
                            'bar'
                          ],
          'location' => [
                          'baz',
                          'biff'
                        ],
          'name' => 'Wally Jones'
        };
2
  • the line print Dumper Load scalar read_file(shift); looks suspicious. Missing doublecolon?
    – gorn
    Aug 17, 2011 at 8:00
  • @gorn It's correct. Dumper() from Data::Dumper, Load() from YAML::XS, scalar is a built in and read_file() from File::Slurp. With the parens it looks like print(Dumper(Load(scalar(read_file(shift))))) which is why I left off the parens.
    – Schwern
    Aug 23, 2011 at 7:35
-1

Can you add some code to your map function to check for the existence of a hash entry and append the new value?

I haven't done Perl in a while, but when I did something like this in the past, I read the file in line by line (while $inputLine = <FILE>) and used split on '=' to load the hash with additional checks to see if the hash already had that key, appending if the entry already existed.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.