in reply to Combinations of lists to a hash

Aww, gee, you made me add one whole line to my program to handle your update, sigh :(

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11107038 use warnings; my @lines = <DATA>; @lines = map /[^=:\s]*,[^=:\s]*/ ? map "$`$_$'", split /,/, $& : $_, @ +lines while "@lines" =~ /,/; my %hash = split ' ', "@lines"; $_ = { split /[=|]/ } for values %hash; use Data::Dump 'dd'; dd \%hash; __DATA__ Prefix1=A,B:c,d value1=5 Prefix2=A:b,c:1,2 value2=6 Prefix4=A:*:1,2 value4a=10|value4b=20 Prefix5,Prefix6=A:*:1,7 value5=10

Outputs:

{ "Prefix1=A:c" => { value1 => 5 }, "Prefix1=A:d" => { value1 => 5 }, "Prefix1=B:c" => { value1 => 5 }, "Prefix1=B:d" => { value1 => 5 }, "Prefix2=A:b:1" => { value2 => 6 }, "Prefix2=A:b:2" => { value2 => 6 }, "Prefix2=A:c:1" => { value2 => 6 }, "Prefix2=A:c:2" => { value2 => 6 }, "Prefix4=A:*:1" => { value4a => 10, value4b => 20 }, "Prefix4=A:*:2" => { value4a => 10, value4b => 20 }, "Prefix5=A:*:1" => { value5 => 10 }, "Prefix5=A:*:7" => { value5 => 10 }, "Prefix6=A:*:1" => { value5 => 10 }, "Prefix6=A:*:7" => { value5 => 10 }, }

I tweaked your original tests to comply with your new specs. Is that valid? If not, you owe us a new test case.

Replies are listed 'Best First'.
Re^2: Combinations of lists to a hash
by tel2 (Pilgrim) on Oct 04, 2019 at 23:23 UTC
    > "...you made me add one whole line to my program to handle your update..."
    Yeah, sorry about that.  I tried to make it harder, honest!

    Yes, that is valid, thank you very much tybalt89!  Nice work - you forced me to vote again.

    I've added a 4th update to my original post, which may help you to simplify your code, while helping me...again.

    BTW, how did you notice my update?  Just by chance or did you get some kind of message?  Just wondering whether I need to bring it to the attention of others who have provided solutions...though I may well go with yours.

    Update: In response to Anonymous Monk's (quite valid) rebuke below, I've realised that my 4th update in my original post simply required the removal of a couple of '=' signs from 1 line of tybalt89's last solution, i.e.:

    #@lines = map /^=:\s*,^=:\s*/ ? map "$`$_$'", split /,/, $& : $_, @lines
    @lines = map /^:\s*,^:\s*/ ? map "$`$_$'", split /,/, $& : $_, @lines
    
    Well, it seems to work, anyway.  If there's better way to make that adjustment, I'm all ears.
      Perlmonks is not a code writing service. Please show some efforts.

        At least the effort to use  <code> ... </code> tags.


        Give a man a fish:  <%-{-{-{-<