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

Hi, I create one XML file which has certain steps of testcases.

<?xml version='1.0'?> <config> <Testcaseid step1="MTPconfiguration.pl" step2="MTPCheckState.pl" step3 +="TM500_RCI.py" step4="ParseTM500Logs.pl" step5="ParseMTPlogs.pl" /> <Testcaseid step1="MTPconfiguration.pl" step2="MTPcheckstateATTN.pl" s +tep3="TM500_RCI.py" step4="ParseTM500log.pl" step5="ParseMTPlogs.pl" +/> </config>

Now i want to read those steps in perl file in a loop. I am trying like the code which i mentioned below. But the problem is as the steps are storing as a hash, i am not able to access in order. for example, 1st it should print in the order as step1, step2, step3...so on but it is printing as step2, step1, step3, step5,step4.... if the content in xml file is written in text file, i am not able to access those values from the text file in perl file. Any help would be appreciated. Thanks in advance

use strict; use warnings; use XML::Simple; use Data::Dumper; my $XML = XMLin("C:\\Users\\Administrator\\Desktop\\Sample\\VERSION2\\ +testcase.xml"); my $testcaseid=$XML->{Testcaseid}; print Dumper(\$testcaseid); my ($testcase,$teststep,$key,$steps); while (($testcase,$teststep)=each @{$testcaseid}){ foreach my $steps (values %{$teststep}) { print "$steps\n"; } }

Replies are listed 'Best First'.
Re: How to get the values from XML/Text file in a specific order?
by Corion (Patriarch) on Jan 19, 2015 at 13:26 UTC

    I don't think that XML specifies an order for attributes. In your case, the attributes are named in a way that you can just use sort to sort the steps. I would do that to sort them and then step through them.

Re: How to get the values from XML/Text file in a specific order?
by locked_user sundialsvc4 (Abbot) on Jan 19, 2015 at 17:21 UTC

    You can also use sort and keys together, e.g.

    my %hash = ( "monkey" => "three", "cobra" => "dance" ); foreach my $key (sort keys %hash) { print "$key is " . $hash{$key} . "\n"; } cobra is dance monkey is see

    The sort verb is applied to the result of the keys verb as applied to %hash.   Therefore, $key will contain each of the hash-keys, in alphabetical order, and this is then used to retrieve the values.   AFAIK, each() can be relied to return keys in a particular order only when the variable is tied to certain types of indexed files.

    Certain cases of sorting can also be specified in an XSLT stylesheet, which is language-independent and can be applied to any XML file e.g. in a browser.   See here.