techtween has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl monks, sorry to ask a so simple question, but ihave been dodging with it for some hours.I have an XML file of the form,

<?xml version="1.0" encoding="UTF-8"?> <testResults version="1.2"> <httpSample t="704" lt="704" ts="1306146504248" s="true" lb="HTTP Requ +est" rc="200" rm="OK" tn="Thread Group 2-1" dt="text" by="411"/> <httpSample t="525" lt="525" ts="1306146505234" s="true" lb="HTTP Requ +est" rc="200" rm="OK" tn="Thread Group 2-2" dt="text" by="411"/> <httpSample t="526" lt="526" ts="1306146506234" s="true" lb="HTTP Requ +est" rc="200" rm="OK" tn="Thread Group 2-3" dt="text" by="411"/> <httpSample t="586" lt="586" ts="1306146611316" s="true" lb="HTTP Requ +est" rc="200" rm="OK" tn="Thread Group 2-1" dt="text" by="411"/> <httpSample t="523" lt="523" ts="1306146612307" s="true" lb="HTTP Requ +est" rc="200" rm="OK" tn="Thread Group 2-2" dt="text" by="411"/> <httpSample t="507" lt="507" ts="1306146613306" s="true" lb="HTTP Requ +est" rc="200" rm="OK" tn="Thread Group 2-3" dt="text" by="411"/> <httpSample t="554" lt="554" ts="1306146614306" s="true" lb="HTTP Requ +est" rc="200" rm="OK" tn="Thread Group 2-4" dt="text" by="411"/> <httpSample t="535" lt="535" ts="1306146615306" s="true" lb="HTTP Requ +est" rc="200" rm="OK" tn="Thread Group 2-5" dt="text" by="411"/> </testResults>

now i need to find and display the maximum and minimum of lt value of each set( that is maximum and minimum values of lt from first three tags seperated by a newline and the next five tags inside a single program also similarly display the average of the two sets. I was able to fetch the lt values alone and display, but somewhere i have made some sort of mistake which devoids me from obtaining the desired output. kindly help.. for your information the code i used is as follows(to get lt value),

use strict; use warnings; use XML::Simple; use Data::Dumper; # create object my $xml_data = new XML::Simple (KeyAttr=>[]); # read XML file my $file_path='c:\catalog1'; my $data = $xml_data->XMLin($file_path); # dereference hash reference # access <httpSample> array foreach my $e (@{$data->{httpSample}}) { my @values = $e->{lt}; print "Lt_Value: ", @values, "\n"; print "\n"; }

Replies are listed 'Best First'.
Re: find min, max and average
by bart (Canon) on May 24, 2011 at 11:11 UTC
    You should loop over all lt values and find the mininimum and maximum values, or you can cheat and use List::Util to give you minimum and maximum from a list

    However, this looks wrong:

    my @values = $e->{lt};
    You'll get just one value. Try this for starters:
    my @values; foreach my $e (@{$data->{httpSample}}) { push @values, $e->{lt}; print "Lt_Value: ", $e->{lt}, "\n"; }
    Now you have an array of all values in @values. The rest is now a piece of cake (you'll have to install Scalar-List-Utils if you don't have it already):
    use List::Util qw(min max sum); print "Min Lt_Value: ", min(@values), "\n"; print "Max Lt_Value: ", max(@values), "\n"; if(@values) { print "Average Lt_Value: ", (sum(@values)/@values), "\n"; } else { print "No values so no average.\n"; }
    n.b. List::Util doesn't have an average() or avg() function, so I have to emulate one using sum() and dividing by the number of elements.
      Thank you monk.. got it right now.

        The issue that i need to get fixed with is that there will be five lines of tags(httpSample tags) then a empty line then another five lines of tag(httpSample tags) then an empty line and so on it goes in this format. Now i need to calculate the minimum and maximum of these each set of five lines(lt value of httpSample tags) seperately and display it.( for the same code listed above, in this thread). Since there is an empty line in between each set of five tags i couldnt figure out how to retrieve each set of five tags into an array and display the same.

Re: find min, max and average
by Corion (Patriarch) on May 24, 2011 at 10:47 UTC

    How does your code fail?

    What should it output?

    What does it output instead?

      i meant to say that i got the lt value using this code. But now i need to find the maximum lt value, minimum lt value, average of all lt values. i need to find these three for all the three sets of tags seperately(then a newline) and then the five set of tags seperately, inside a single program. this is what i couldnt do. so kindly help. .

        Copy and modify your search for the minimum to find the maximum, and output the maximum after that.

        After you have found the minimum and maximum, you can use almost the same technique you used to find the minimum and the maximum to also calculate the average.