Let’s say that I have a hashref whose Data::Dumper output looks like this:
$VAR1 = {
foo_0 => 'foo_zero',
foo_1 => 'foo_one',
bar_0 => 'bar_zero',
bar_1 => 'bar_one'
}
I would like to split this hash into two based on its keys as shown below but I don’t know how to do this:
$VAR1 = {
foo_0 => 'foo_zero',
foo_1 => 'foo_one'
},
$VAR2 = {
bar_0 => 'bar_zero',
bar_1 => 'bar_one'
}
The keys of the first hash match /foo_[\d]/ while those of the second hash match /bar_[\d]/.
If you could kindly tell me how to do this (or hint me some search keywords) I would appreciate it.
Regards,
Christopher Smith
The other solutions posted so far work, but are quick and dirty. They need to be changed when the input patterns change, and assume only two patterns. This generalised solution does not suffer from that: it needs no change, and it takes any number of patterns.