![]() |
|
Syntactic Confectionery Delight | |
PerlMonks |
Re: DBI's selectall_hashref and nested foreach loopsby anonymized user 468275 (Curate) |
on Jul 06, 2005 at 11:14 UTC ( #472759=note: print w/replies, xml ) | Need Help?? |
To avoid having one foreach per level in the hash, I would write a recursive hash traversal subroutine as in this (tested) example, which uses a single foreach in a subroutine that calls itself when it finds a hash reference instead of a value: Update 1: the point of all this is not how many times foreach is actually executed but to avoid having to code differently for different nesting levels of the hash. Update 2: modified to show how to make the actual processing of the hash flexible. The assignment into a flat array is just to demonstrate that all the values and none of the keys of the hash were covered. A slight change can make it do something else at each node. Update 3: where the code examines ref($val), it could also be adapted to process arrays within hashes, by creating an extra branch for where ref($val) eq 'ARRAY'.
Output:
One world, one people
In Section
Seekers of Perl Wisdom
|
|