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).
%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 value3I 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=20The 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=10The 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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |