I concur with Ovid on srawls' solution: very slick indeed.
The reason I'm trying to get at a scalar value for the subkeys
in a %HashOfHashes without looping is because I'm already
iterating through the keys/subkeys.
What I'm really trying to do: given a %HoH, iterate through
the subkeys and display each $interval group of subkeys
(where $interval is a positive integer). Of course, when
I get to the end, I'll need to kick out the last group even
though the counter never gets to $interval.
Example and pseudocode: my %HoH contains 97 subkey-value pairs
(but I don't it's 97).
$interval = 10;
foreach $key (keys %HoH) {
if ($interval % $counter == 0) {
display(\%tmp);
$counter = 0; # reset the counter
}
foreach $subkey (keys %{$HoH{$key}}) {
$tmp{++$counter} = "$key -- $subkey";
}
}
The problem I run into is when $counter is a remainderless
divisor of $interval (ie, $counter = 1). I can kludge around
this obstacle by doing a sort of --$secondary_counter decrement
if I knew the total number of subkeys with which I could compare
$secondary_counter. This is where srawls' golfing looks great
but then I'd be iterating over the hash twice.
Muchas gracias,
"dogs luv me cuz i'm crazy sniffable / i bet you never knew i got the ill peripherals"
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.