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

dear monks i hav a code for convert a xml file as hash using XML::Simple but the hash output has changed the sequence of the XML tags pls tel me hw can i get the output without change the sequence

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use XML::Simple; my %xhash=(); my $file= $ARGV[0]; my $simple = XML::Simple->new(); my $xhash = $simple->XMLin($file); print Dumper($xhash); exit; open FH,"config.ini" or die $!; my $line; my %c_hash=(); my $c_hashRef = ''; my @tmpA = (); sub recur($); foreach $line (<FH>){ $line=~s/^(\s+)(.*)/$2/i; $line=~s/(.*?)(\s+)$/$2/i; chomp($line); if ($line ne ''){ $_=$line; $c_hashRef = ''; my @array = split(m/\|\|/, $line); print Dumper($c_hashRef); ($c_hashRef,@tmpA) = recur(\@array); } } print Dumper($c_hashRef); exit; sub recur($) { my $arrRef = $_[0]; my @procArr = @$arrRef; my $assign = ''; my %c_hash = (); my @tmpArr = @procArr; shift(@tmpArr); my $arr = $procArr[0]; if ($arr=~m/\[(.*)=(.*)\]/ig) { $assign = $1; my $tmpVal = ''; $tmpVal = $2; $tmpVal =~ s/\=(.*?)/$1/ig; $c_hash{$assign}{'repval'} = $tmpVal; ($c_hash{$assign}{'addval'},@tmpArr) = recur(\@tmpArr); $arr = shift(@tmpArr); if ($arr) { if ($arr=~m/\[(.*)=(.*)\]/ig) { my $key = $1; my $tmpVal = ''; $tmpVal = $2; $tmpVal =~ s/\=(.*?)/$1/ig; $c_hash{$key}{'repval'} = $tmpVal; ($c_hash{$key}{'addval'},@tmpArr) = recur(\@tmpArr +); $arr = shift(@tmpArr); } } } return (\%c_hash, @tmpArr); }

this is my output

$VAR1 = { 'chapter' => { 'subchap1' => { 'head1' => 'Prostaglandins', 'para' => [ { 'sub' => '2', 'content' => [ 'The p +rostaglandins are a group of biologically active derivatives of arach +idonic acid often referred to as eicosanoids for their basic 20-carbo +n atom structure. The two major pathways of eicosanoid metabolism are + the cyclooxygenase pathway, which yields the prostaglandins and thro +mboxanes, and the lipoxygenase pathway, which yields the leukotrienes +. A minor pathway termed epoxy-genase yields epoxides, which have rec +eived scant attention in the central nervous system (CNS). Arachidoni +c acid is synthesized on demand from dietary linoleic acid by either +a G protein-regulated phospholipase A', ' or d +iglyceride lipase activation (', '), an +d it yields a very broad array of bioactive metabolites, as shown in +', '. The + three major groups of arachidonic acid-derived metabolites are the p +rostaglandins, thromboxanes, and leukotrienes.' ], 'hotlink' => [ { 'xref' + => { + 'xref' => 'f012001', + 'xidtype' => 'figure' + }, 'conte +nt' => "Fig. 12\x{2013}1" }, { 'xref' + => { + 'xref' => 'f012002', + 'xidtype' => 'figure' + }, 'conte +nt' => "Figure 12\x{2013}2" } ] }, { 'c' => [ '\'', "\x{2014}", "\x{2014}" ], 'content' => [ 'Histo +rically, the earliest effect of prostaglandins arose with the recogni +tion in the 1930s that fresh semen could induce contraction of myomet +rial muscle, and the name arose from the factor', 's ant +icipated origin. As chemical detection methods improved in the 1950s, + two classes of prostaglandins were recognized', 'a PGE + class that was soluble in ether and a PGF class soluble in phosphate + buffer (', ' in S +wedish)', 'and o +ne of their sites of synthesis localized to seminal vesicles. Subsequ +ent work indicated that virtually every organ could manufacture prost +aglandins, and several distinct synthetic pathways were recognized. A + major advancement occurred when John Vane proposed that aspirin and +several other nonsteroidal anti-inflammatory drugs (NSAIDs) worked by + inhibiting the prostaglandin-synthesizing enzyme cyclooxygenase. Wor +k in the 1990s revealed that a second cyclooxygenase (COX-2) (and per +haps a third) was also present in the CNS. Since this enzyme was show +n to be induced by inflammatory cytokines, it immediately suggested a + separate target for relief of inflammatory pain, with the potential +for reduced symptoms from the gastrointestinal tract irritation typic +ally evoked by NSAIDs. COX-2 is also induced ', ' by t +he neurotransmitter GLU and inhibited by glucocorticoids. Unfortunate +ly, prolonged use of COX-2 inhibitors has untoward cardiovascular sid +e effects, leading to multiple litigious claims and diverting attenti +on from this once-promising therapeutic modification.' ], 'emphasis' => [ { 'cont +ent' => 'fosfat', 'styl +e' => 'it' }, { 'cont +ent' => 'in vitro', 'styl +e' => 'it' } ] }, { 'c' => [ "\x{3b3}", '[', ']', "\x{201c}", "\x{201d}" ], 'content' => [ "It is + well known that the eicosanoids, particularly the prostaglandin seri +es, play an important modulatory role in nervous tissue, but it has b +een difficult to write a lucid account of specifically how and where +they act. This is primarily due to the fact that they are not stored +in tissues, nervous or other, but synthesized on demand, particularly + in pathophysiological conditions. They act briefly (some with a half +-life of seconds) and at extremely low concentrations (10\x{2212}", ' M). +Although indomethacin is a good inhibitor of cyclooxygenase-1, blocki +ng the conversion of arachidonic acid to prostaglandins, there are fe +w specific inhibitors available to block lipoxygenase and epoxygenase +. Thus, although it had been postulated that the E series of prostagl +andins modulates noradrenergic release, blocks the convulsant activit +y of pentylenetetrazol, strychnine, and picrotoxin (possibly by incre +asing the level of ', '-amin +obutyric acid ', 'GABA' +, ' in t +he brain), and increases the level of cAMP in cortical and hypothalam +ic slices, these effects were noted ', ' with + the addition of substantial amounts of the prostaglandins. There was + very little evidence in intact animals to support these neuronal fin +dings. Since we skeptics all hold ', ' veri +tas', ' in h +igher regard, the physiological relevance of the effect was in doubt. +' ], 'emphasis' => [ { 'cont +ent' => 'in vitro', 'styl +e' => 'it' }, { 'cont +ent' => 'in vivo', 'styl +e' => 'it' } ], 'hotlink' => { 'xref' = +> { + 'xref' => 'r012010', + 'xidtype' => 'text' +}, 'sup' => + '10' } }, { 'sub' => '2', 'content' => [ 'Subse +quently, however, direct evidence has established arachidonic acid an +d lipoxygenases as second messengers. The cascade begins with the bin +ding of a neuroactive agent to its receptor. Then, according to findi +ngs from the Axelrod laboratory, the receptor is coupled to G protein +s, which may either activate or inhibit phospholipase A', ', alt +hough this has not been conclusively established for all neural tissu +es. The activated enzyme promotes the release of arachidonic acid, wh +ich will then act intracellularly as a second messenger. Arachidonic +acid and its metabolites can also leave the cell to act extracellular +ly as first messengers on neighboring cells. Eicosanoids have been sh +own to mediate the somatostatin-induced opening of an M channel in hi +ppocampal pyramidal cells and the release of VIP in mouse cerebral co +rtical slices. At the supracellular level, prostaglandins of the E se +ries have been held to be a mediator of fever and prostaglandins of t +he D series as regulators of sleep. It is thus becoming clear, despit +e enormous technological difficulties in assaying eicosanoids, that t +hese agents are major messengers.' ] }, 'Another exciting chapt +er of the arachidonic acid story has been told separately, namely the + endocannabinoids, which are described later in this chapter.' ] }, 'para' => { 'c' => [ "\x{201c}", "\x{201d}", "\x{201c}", "\x{201d}" ], 'content' => [ 'The previous chapters + were devoted to what we might now consider the ', 'classical', ' or perhaps ', 'conventional', ' neurotransmitters. B +y this, we mean those transmitters that are synthesized within the ne +uron (cell body or synaptic terminal) that releases them by activity- +dependent, Ca', '-dependent mechanisms + to act on discrete receptors largely, but not exclusively, on the ne +uron, smooth muscle, or gland cell opposite the nerve terminal. Howev +er, as the wheels of progress have turned, it seems that once again t +he more we learn about inter-cellular communication in the nervous sy +stem, the more complicated the situation becomes. The purine signals +discussed in ', ' provided an appetize +r by acting both pre- and postsynaptically, as do many of the other c +lassical transmitters. Nevertheless, through improved methods of subs +tance identification and detection of signal responsiveness, several +potent interneuronal signals have been recognized that are not stored + in vesicles, or even stored at all, but rather seem to be synthesize +d and released on demand to act more broadly than the immediate-relea +sing neuron terminals and to modify the effectiveness of the conventi +onal interneuronal signals.' ], 'hotlink' => [ { 'xref' => { 'xref' => 'r +012002', 'xidtype' => + 'text' }, 'sup' => { 'c' => '+', 'content' => +'2' } }, { 'xref' => { 'xref' => 'c +011', 'xidtype' => + 'text' }, 'content' => 'Chapter +11' } ] }, 'title' => '12 Other Interneuronal Signals', 'id' => 'c012', 'chapnum' => '12' } };

input xml file

<?xml version="1.0"?> <book> <chapter chapnum="12" id="c012"> <title>12 Other Interneuronal Signals</title> <para>The previous chapters were devoted to what we might now consider + the <c>&#8220;</c>classical<c>&#8221;</c> or perhaps <c>&#8220;</c>c +onventional<c>&#8221;</c> neurotransmitters. By this, we mean those t +ransmitters that are synthesized within the neuron (cell body or syna +ptic terminal) that releases them by activity-dependent, Ca<hotlink>< +xref xref="r012002" xidtype="text"/><sup>2<c>+</c></sup></hotlink>-de +pendent mechanisms to act on discrete receptors largely, but not excl +usively, on the neuron, smooth muscle, or gland cell opposite the ner +ve terminal. However, as the wheels of progress have turned, it seems + that once again the more we learn about inter-cellular communication + in the nervous system, the more complicated the situation becomes. T +he purine signals discussed in <hotlink><xref xref="c011" xidtype="te +xt"/>Chapter 11</hotlink> provided an appetizer by acting both pre- a +nd postsynaptically, as do many of the other classical transmitters. +Nevertheless, through improved methods of substance identification an +d detection of signal responsiveness, several potent interneuronal si +gnals have been recognized that are not stored in vesicles, or even s +tored at all, but rather seem to be synthesized and released on deman +d to act more broadly than the immediate-releasing neuron terminals a +nd to modify the effectiveness of the conventional interneuronal sign +als.</para> <subchap1><head1>Prostaglandins</head1> <para>The prostaglandins are a group of biologically active derivative +s of arachidonic acid often referred to as eicosanoids for their basi +c 20-carbon atom structure. The two major pathways of eicosanoid meta +bolism are the cyclooxygenase pathway, which yields the prostaglandin +s and thromboxanes, and the lipoxygenase pathway, which yields the le +ukotrienes. A minor pathway termed epoxy-genase yields epoxides, whic +h have received scant attention in the central nervous system (CNS). +Arachidonic acid is synthesized on demand from dietary linoleic acid +by either a G protein-regulated phospholipase A<sub>2</sub> or diglyc +eride lipase activation (<hotlink><xref xref="f012001" xidtype="figur +e"/>Fig. 12&#8211;1</hotlink>), and it yields a very broad array of b +ioactive metabolites, as shown in <hotlink><xref xref="f012002" xidty +pe="figure"/>Figure 12&#8211;2</hotlink>. The three major groups of a +rachidonic acid-derived metabolites are the prostaglandins, thromboxa +nes, and leukotrienes.</para> <para>Historically, the earliest effect of prostaglandins arose with t +he recognition in the 1930s that fresh semen could induce contraction + of myometrial muscle, and the name arose from the factor<c>&#39;</c> +s anticipated origin. As chemical detection methods improved in the 1 +950s, two classes of prostaglandins were recognized<c>&#8212;</c>a PG +E class that was soluble in ether and a PGF class soluble in phosphat +e buffer (<emphasis style="it">fosfat</emphasis> in Swedish)<c>&#8212 +;</c>and one of their sites of synthesis localized to seminal vesicle +s. Subsequent work indicated that virtually every organ could manufac +ture prostaglandins, and several distinct synthetic pathways were rec +ognized. A major advancement occurred when John Vane proposed that as +pirin and several other nonsteroidal anti-inflammatory drugs (NSAIDs) + worked by inhibiting the prostaglandin-synthesizing enzyme cyclooxyg +enase. Work in the 1990s revealed that a second cyclooxygenase (COX-2 +) (and perhaps a third) was also present in the CNS. Since this enzym +e was shown to be induced by inflammatory cytokines, it immediately s +uggested a separate target for relief of inflammatory pain, with the +potential for reduced symptoms from the gastrointestinal tract irrita +tion typically evoked by NSAIDs. COX-2 is also induced <emphasis styl +e="it">in vitro</emphasis> by the neurotransmitter GLU and inhibited +by glucocorticoids. Unfortunately, prolonged use of COX-2 inhibitors +has untoward cardiovascular side effects, leading to multiple litigio +us claims and diverting attention from this once-promising therapeuti +c modification.</para> <para>It is well known that the eicosanoids, particularly the prostagl +andin series, play an important modulatory role in nervous tissue, bu +t it has been difficult to write a lucid account of specifically how +and where they act. This is primarily due to the fact that they are n +ot stored in tissues, nervous or other, but synthesized on demand, pa +rticularly in pathophysiological conditions. They act briefly (some w +ith a half-life of seconds) and at extremely low concentrations (10&# +8722;<hotlink><xref xref="r012010" xidtype="text"/><sup>10</sup></hot +link> M). Although indomethacin is a good inhibitor of cyclooxygenase +-1, blocking the conversion of arachidonic acid to prostaglandins, th +ere are few specific inhibitors available to block lipoxygenase and e +poxygenase. Thus, although it had been postulated that the E series o +f prostaglandins modulates noradrenergic release, blocks the convulsa +nt activity of pentylenetetrazol, strychnine, and picrotoxin (possibl +y by increasing the level of <c>&#947;</c>-aminobutyric acid <c>[</c> +GABA<c>]</c> in the brain), and increases the level of cAMP in cortic +al and hypothalamic slices, these effects were noted <emphasis style= +"it">in vitro</emphasis> with the addition of substantial amounts of +the prostaglandins. There was very little evidence in intact animals +to support these neuronal findings. Since we skeptics all hold <c>&#8 +220;</c><emphasis style="it">in vivo</emphasis> veritas<c>&#8221;</c> + in higher regard, the physiological relevance of the effect was in d +oubt.</para> <para>Subsequently, however, direct evidence has established arachidon +ic acid and lipoxygenases as second messengers. The cascade begins wi +th the binding of a neuroactive agent to its receptor. Then, accordin +g to findings from the Axelrod laboratory, the receptor is coupled to + G proteins, which may either activate or inhibit phospholipase A<sub +>2</sub>, although this has not been conclusively established for all + neural tissues. The activated enzyme promotes the release of arachid +onic acid, which will then act intracellularly as a second messenger. + Arachidonic acid and its metabolites can also leave the cell to act +extracellularly as first messengers on neighboring cells. Eicosanoids + have been shown to mediate the somatostatin-induced opening of an M +channel in hippocampal pyramidal cells and the release of VIP in mous +e cerebral cortical slices. At the supracellular level, prostaglandin +s of the E series have been held to be a mediator of fever and prosta +glandins of the D series as regulators of sleep. It is thus becoming +clear, despite enormous technological difficulties in assaying eicosa +noids, that these agents are major messengers.</para> <para>Another exciting chapter of the arachidonic acid story has been +told separately, namely the endocannabinoids, which are described lat +er in this chapter.</para></subchap1> </chapter> </book>

here the tags are changed their places i want as the sequence output hash how can i get please help me to guide

Replies are listed 'Best First'.
Re: hash sequence error
by CountZero (Bishop) on Jun 15, 2010 at 06:09 UTC
    here the tags are changed their places
    What did you expect? A hash will associate a key with a value; its purpose is not to preserve a sequence which for a hash has no meaning.

    From the XML::Simple::FAQ:

    My element order is getting jumbled up

    If you read an XML file with XMLin() and then write it back out with XMLout(), the order of the elements will likely be different. (However, if you read the file back in with XMLin() you'll get the same Perl data structure).

    The reordering happens because XML::Simple uses hashrefs to store your data and Perl hashes do not really have any order.

    It is possible that a future version of XML::Simple will use Tie::IxHash to store the data in hashrefs which do retain the order. However this will not fix all cases of element order being lost.

    If your application really is sensitive to element order, don't use XML::Simple (and don't put order-sensitive values in attributes).

    Update: added quote from FAQ

    PS: please only show the code relevant to your problem (i.e. in this case the first ten lines) and restrict your data to a minimal but still relevant sample.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: hash sequence error
by CountZero (Bishop) on Jun 21, 2010 at 06:49 UTC
    Your message in the CB:
    satzbu says: Re Re: hash sequence error. Sir I understand my application is highly sensitve for xml sequence I tried with Tie::TxHash but it cant secure the order of the inner most values of the xml file. If dont use XML::Simple means which one is suitable for me.
    .

    It is difficult to say which of the XML-modules will help you here. If you want the Perlmonks to help you, you should give us some more info about what you are trying to achieve. Just extracting all the XML-nodes into a data-structure is maybe not what is needed. What is you rultimate aim?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: hash sequence error
by CountZero (Bishop) on Jun 23, 2010 at 15:50 UTC
    Your message in the CB:
    satzbu says Re Re: hash sequence error. Thank you sir. My ultimate aim is store an XML file in hash without changing the structure and I have some process over the hash value and then retrieve the hash value as an xml. This is my ultimate aim sir please help me.
    A hash is not the right way to store data if the sequence is important.

    But when you intend to transform or select parts of the XML-file and the result must be another XML file, perhaps you should look into other solutions, such as XSLT or XML::XPath.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James