in reply to Re: Hash Values into Array
in thread Hash Values into Array

"for $my os (values %info) {"

Two corrections:

 

"This should also work if you have several errors possible for one testcase, such as 'error1', 'error2', etc."

You're manufacturing data that the OP hasn't specified or even hinted at. I don't think this is a good idea. Furthermore, you're not targeting the specific key (i.e. error) that the OP did specify; you are, in fact, targeting every key that exists. Consider this scenario (using your code, with the corrections shown above):

$ perl -Mstrict -Mwarnings -le ' my $info = { win => { t1 => { error => "missing arg" }, t2 => { skipped => "no network" } }, nix => { t1 => { error => "wrong type" }, t2 => { success => 1 } } }; my @errors; for my $os (values %$info) { for my $testcase (values %$os) { push @errors, values %$testcase } } print for @errors; ' no network missing arg wrong type 1

-- Ken

Replies are listed 'Best First'.
Re^3: Hash Values into Array
by Laurent_R (Canon) on Aug 17, 2013 at 08:08 UTC

    You're manufacturing data that the OP hasn't specified or even hinted at. I don't think this is a good idea. Furthermore, you're not targeting the specific key (i.e. error) that the OP did specify; you are, in fact, targeting every key that exists.

    You're right, Ken, but the OP did not provide any data, so I made some assumptions about the contents of the data structure. I quickly populated a nested data structure and tested the solution under the Perl debugger. The idea was only to show that using the values function rather than the keys function saved some typing and made the solution somewhat simpler.