Beloved Monks,

After hours of messing around with various attempts at this (which I'd rather not bore you with), I seek your help a script which takes input like this:

Prefix1=A,B:c,d value1
Prefix2=A:b,c:1,2 value2
(That's a space before each value).
And outputs all combinations of the comma separated items within the colon separated groups, and puts the result in a hash like this:
%hash =
{
  'Prefix1=A:c' => 'value1',
  'Prefix1=A:d' => 'value1',
  'Prefix1=B:c' => 'value1',
  'Prefix1=B:d' => 'value1',
  'Prefix2=A:b:1' => 'value2',
  'Prefix2=A:b:2' => 'value2',
  'Prefix2=A:c:1' => 'value2',
  'Prefix2=A:c:2' => 'value2'
};
There could be any number of colon separated groups and comma separated items within each group, so I expect a recursive function would be apt, (at least that's what I was trying to do). I'm not wanting the overhead of a module, please.

I've been using a solution which I coded ages ago, but that only handles 2 groups (using a for loop within a for loop), but now I want something which will handle any number of groups.

UPDATE:
Thank you all for your answers, many of which I have voted for.
Some things which I failed to mention (sorry!) when I posted the original are:

#1. Any group can be an '*', e.g.:

Prefix3=A:*:1,2 value3
I notice that this poses problems for the 'glob' solutions, and I think some have suggested solutions to that.  If you have a simple fix, please update your solution with it, otherwise I'll see what I can come up with.  I found that escaping the * like \* in the input is one option, but not ideal for me.

#2. The value (on the right) can be a '|' separated list, e.g.:

Prefix4=A:*:1,2 value4a=10|value4b=20
The above should result in %hash entries like this:
{
  'Prefix4=A:*:1' => {
                   'value4a' => '10',
                   'value4b' => '20'
                     },
  'Prefix4=A:*:2' => {
                   'value4a' => '10',
                   'value4b' => '20'
                     },
}
In fact, all input, whether it has '*' or not, and whether it has single or multiple values, is meant to create a hash of hashes like the above example.

#3. Some day I might want to include the prefixes in the things which can contain lists, e.g.:

Prefix5,Prefix6=A:*:1 value5=10
The result would be:
{
  'Prefix5=A:*:1' => {
                   'value5' => '10',
                     },
  'Prefix6=A:*:1' => {
                   'value5' => '10',
                     },
}
I'm not worried if no one wants to handle that possibility, but if you're willing then great!  (It looks as if AnomalousMonk was already heading that way. (Update: And tybalt89 has now done it.))

#4. I'm just realising now that I think I'm able to simply the requirements by doing away with the '=' and using ':' in place of it, so that should make the code easier. For eample:

Prefix7:A:*:1 value7=10

Sorry I didn't mention any of this originally. The hash of hashes item slipped my mind until I'd posted, then it was 1 AM so I went to bed, and I was hoping I could manage the adjustment to any solutions myself, but at this stage I'm struggling to understand your solutions enough to do so, though I love their concise elegance!

This is for a web application that I've been working on for years, and it is invoked during each page submission, so speed is important.
Thanks again.
Tel2


In reply to Combinations of lists to a hash by tel2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.