ImJustAFriend has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks. I have an issue I need to turn around fairly quickly. I have been working on parsing a log file. I got the log (finally) into standard "key=value" format and I was just putting them all into a hash. That was fine until I saw there were lines like this in the log:
key_a = abc123 key_a = abc456
So now I have a duplicate value situation. Here is my relevant code at this point:
foreach my $bline (@blocklines) { print DEBUG "bline: \"$bline\"\n"; my ($k, $v) = split '=', $bline; $kvhash{$k} = $v; }
How would I handle the multivalue needs in this structure. I am open to just about anything... thank you!!

Replies are listed 'Best First'.
Re: Need Your Help Quickly, Please - Hash Issues
by Corion (Patriarch) on Jan 10, 2019 at 13:27 UTC

    Maybe you can explain to us how you need to handle the multiple values.

    Should the first value be kept?

    Should the last value be kept?

    Should all values be kept?

    Should no value be kept because it is not clear which value is the correct one?

    What should happen if there is more than one value?

    What should happen if there are more than two values?

    Maybe you want to learn about Perl Data Structures, especially Hashes of Arrays?

Re: Need Your Help Quickly, Please - Hash Issues
by Tux (Canon) on Jan 10, 2019 at 13:27 UTC
    foreach my $bline (@blocklines) { chomp ($bline); $bline =~ s/^\s+//; $bline =~ s/\s+$//; print DEBUG "bline: \"$bline\"\n"; my ($k, $v) = split m/\s*=\s*/ => $bline; $k or next; push @{$kvhash{$k}}, $v; }

    Enjoy, Have FUN! H.Merijn
      Looking like this will work. Thanks Tux!
Re: Need Your Help Quickly, Please - Hash Issues
by BillKSmith (Monsignor) on Jan 11, 2019 at 04:51 UTC
Re: Need Your Help Quickly, Please - Hash Issues
by 1nickt (Canon) on Jan 10, 2019 at 14:46 UTC

    Hi, for merging hashes use Hash::Merge.

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Need Your Help Quickly, Please - Hash Issues
by LanX (Saint) on Jan 10, 2019 at 15:52 UTC
    > Need Your Help Quickly, Please -

    please don't litter your titles with such needless stuff.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice