I'm parsing an file, and parts of it is a records thing, the format is like:
CategoryA--
5: UserA
6: UserB
7: UserC
CategoryB--
4: UserA
5: UserB
I want to move it to a hash that looks like:
{ UserA => { CategoryA => 5, CategoryB => 4, },
UserB => { CategoryA => 6, CategoryB => 5, },
UserC => { CategoryA => 7, },
}
How do I do regex on this?
Edit: It does not have to be purely only regex - just in perl and loops would be good too.
You need two regexes, one to identify new categories and one to parse user records.
Or, if you have Perl 5.10 or later, you can use named captures with one regex:
This perl code seems to do what your looking for (mostly with one change). I laied out the data structure a bit differently but not much.
Not exactly trying to golf here, but it can be done in a single alternation:
Data::Dumper
(actually Smart::Comments) shows the output:RESULT:
This will split it up for you.