I have a perl script which evaluates an xml script using the keys()
function. My script is written in such a way, attributes/tags in xml are not coming in order. Does the keys function in perl, evaluate randomly??
Ex:
if( (keys %{$data})[0] eq 'fileFooter' and
(keys %{$data->{fileFooter}})[0] eq 'measCollec' and
(keys %{$data->{fileFooter}->{measCollec}})[0] eq 'endTime' and
(keys %{$data})[1] eq 'fileHeader' and
(keys %{$data->{fileHeader}})[0] eq 'measCollec' and
(keys %{$data->{fileHeader}->{measCollec}})[0] eq 'beginTime'
Here the fileFooter attribute in XML file is coming at the end and the fileHEader is coming at the beginning. In this case perl will work fine??
Please find the script in below link : https://docs.zoho.com/writer/published.do?rid=x6jdb8effa7ba9b0140258c9b3b1fb9617386
Please find the XML file in below link :
https://docs.zoho.com/writer/ropen.do?rid=x6jdbcd99dd2df097455f99fa2907a84620ee
One way to think about this is to remember that you access the "values" in an array by the ordered elements in the data structure (which is why you use
$array[2]
,@array[1,2,3]
or$array_ref->[3]
). With a hash you access "values" by their respective keys which could be in any order because you are accessing them in an "unordered" way.See Why do hash keys have different order when printing? and other posts linked to under the "Related" sidebar for more detailed discussion.