I’m trying to write a sub that can take a list of hashes and create a nested list based on the contents of an arbitrary number of fields. I just can’t get the recursion setup correctly. I’m getting a bunch of bug data out of a database and want to group the data on an arbitrary list of fields (team, priority, etc). I really don’t have any example code that I think is even getting close enough
Example below
I have the following DS:
$ds =
[
{
foo => 'A',
bar => 'B',
baz => 'C',
},
{
foo => 'A',
bar => 'B',
baz => 'F',
},
{
foo => 'A',
bar => 'D',
baz => 'G',
},
{
foo => 'R',
bar => 'J',
baz => 'G',
}
]
Given the following function call
# prototype groupBy(data, field-1,field-2,field-n)
groupBy($ds,'foo','bar');
I want the following output
$res = {
A => {
B => [
{
foo => 'A',
bar => 'B',
baz => 'C',
},
{
foo => 'A',
bar => 'B',
baz => 'F',
}
],
D => [
{
foo => 'A',
bar => 'D',
baz => 'G',
}
],
},
R => {
J => [
{
foo => 'R',
bar => 'J',
baz => 'G',
}
}
};
This is quite straightforward using a recursive approach
The following code demonstrates
output