Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Code for parsing an array of hashes

by balajinagaraju (Sexton)
on Apr 04, 2012 at 05:05 UTC ( #963365=perlquestion: print w/ replies, xml ) Need Help??
balajinagaraju has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am facing a problem to parse the below datastructure,basically its an array of hashes.All i want to do is write a script which will access testcase1 and perform each steps in an order. I need to access step1 from testcase1 and trigger the command with the given parameters , the command here is the name of a function module which is a part of another perl script which i have written and the parameters are the arguments for my function. Once step1 is completed i need to perform step2 on my application, i also need a way to synchronise my steps every step has to be executed based on the return status of the previous step or result of the previous step. If step1 fails it has to exit and should not perform step2. After testcase1 is executed it has to goto testcase2 and perform the operations. Can anyone please suggest me how i can implement a solution for this.?Looking forward for your help

@VAR1 = { 'testcase1' => { 'step1' => { 'command' => 'Launchapplication ', 'param1' => 'executablepath', 'param2' => 'dirpath' }, 'step2' => { 'command' => 'SelectMenu' }, 'step3' => { 'command' => 'Changemode', 'param1' => '100' }, 'step4' => { 'command' => 'CaptureRectanlge', 'param1' => '3', 'param2' => '96', 'param3' => '726', 'param4' => '580' }, 'step5' => { 'command' => 'CompareImage', 'image1' => 'D:\\\\img1.jpg', 'image2' => 'D:\\\\img2.jpg ' } }, 'testcase2' => { 'step1' => { 'command' => 'Launchapplication ', 'param1' => 'executablepath', 'param2' => 'dirpath' }, 'step2' => { 'command' => 'Dothis' }, 'step3' => { 'command' => 'Changemode', 'param1' => '100' }, 'step4' => { 'command' => 'CaptureRectanlge', 'param1' => '3', 'param2' => '96', 'param3' => '726', 'param4' => '580' }, 'step5' => { 'command' => 'CompareImage', 'image1' => 'D:\\\\img1.jpg', 'image2' => 'D:\\\\img2.jpg ' } }

Comment on Code for parsing an array of hashes
Download Code
Re: Code for parsing an array of hashes
by Riales (Hermit) on Apr 04, 2012 at 05:40 UTC

    It's a little bit not as straightforward as it could be because you're using a HoH when an AoA is probably far more appropriate. This is because the sequence of elements in a HoH is pretty much arbitrary; things won't stay in the order of testcase1, testcase2 or step1, step2, step3, step4, step5. Then, you can just loop over your test cases and for each test case loop over each of its steps.

Re: Code for parsing an array of hashes
by mbethke (Hermit) on Apr 04, 2012 at 06:07 UTC
    As you wrote it, it's an array of hashes of hashes. Actually it would make more sense to write it as an array of arrays of hashes to be clear about the ordering, otherwise you'd have to sort the keys when retrieving them and the sort order is sometimes not very intuitive---e.g. does "step10" come before or after "step1", or do you have to number the steps starting with "step01" then? The same goes for the parameter lists (and they're so much easier to both write and use if they're arrays already), so I'd make it something like this:
    use strict; use warnings; use 5.010; my @tests = ( [ # first testcase { # first step command => 'Launchapplication', params => [ qw/ executablepath dirpath / ], }, { # second step command => 'SelectMenu', }, # ... ], [ # second testcase { # first step command => 'Launchapplication', params => [ qw/ executablepath dirpath / ], }, { # second step command => 'Dothis', }, # ... ] ); my $package = "My::Package"; CASE: for my $ncase (0 .. $#tests) { my $case = $tests[$ncase]; foreach my $nstep (0 .. $#$case) { my $step = $case->[$nstep]; if(defined $step->{command}) { my $ok = 1; try { no strict 'refs'; "${package}::$step->{command}"->(@{ $step->{params} // + [] }); } catch { warn sprintf("FAILURE test %d step %d failed, aborting +", $ncase+1, $nstep+1); $ok = 0; # can't use "last" here due to try/catch limi +tation }; last CASE unless $ok; } else { warn sprintf("BUG: `command' undefined in test %d step %d" +, $ncase+1, $nstep+1); last CASE; } } }
    Edit: oops, Riales beat me to it :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963365]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2014-12-26 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (168 votes), past polls