In my project i am reading the XML file and writing into new file updating few data in it, for which i have used hash to check if record exist, I have written the code and its working well, but i was think if there is any smart solution I mean less code to do same task!!!! here is my code:-
sub dispatch { my $HRR_resource = shift; my $HRR_wnt = shift; my $HRR_unx = shift; my $HRR_Scomponent = shift; my $HRR_Wcomponent = shift; my $Pkey; my $Wkey; my $Ukey; my $SCkey; my $WCkey; my $Flag; open(FR, "<$source_file")|| die "Unable to open input file: $sourc +e_file $!"; open(FW, ">$dest_file")|| die "Unable to open output file: $dest_f +ile $!"; while (my $line=<FR>) { $line =~tr/\r\n//d; if ($line) { $Flag = 1; if ($line =~ /PLATFORM_INDEPENDENT/){$Pkey = $line}; if ($Pkey) { $Flag = undef; $Pkey =~ s/\s*//; if($Pkey ne '</PLATFORM_INDEPENDENT>') { if ($line =~ /Id="(.*?)".*?>(.*?)</) { my $key_id = $1; my $key_val = $2; if (exists $$HRR_resource{$key_id}) { my $val = $$HRR_resource{$key_id}; $val =~ /.*?>(.*?$)/; $val = $1; $line =~ s/>.*?</>$val</; print FW "$line\n"; delete($$HRR_resource{$key_id}); } else { print FW "$line\n"; } } else { print FW "$line\n"; } } if ($line =~ /<\/PLATFORM_INDEPENDENT>/) { foreach my $addline(keys %$HRR_resource) { print FW "<RESOURCE Id=\"$addline\" $$HRR_resource +{$addline}</RESOURCE>}\n"; } print FW "$line\n"; $Pkey = undef; } } if ($line =~ /WNT/){$Wkey = $line}; if ($Wkey) { $Flag = undef; $Wkey =~ s/\s*//; if ($Wkey eq '<WNT>' && $Wkey ne '</WNT>') { if ($line =~ /Id="(.*?)".*?>(.*?)</) { my $key_id = $1; my $key_val= $2; if (exists $$HRR_wnt{$key_id}) { my $val = $$HRR_wnt{$key_id}; $val =~ /.*?>(.*?$)/; $val = $1; $line =~ s/>.*?</>$val</; print FW "$line\n"; delete($$HRR_wnt{$key_id}); } else { print FW "$line\n"; } } else { print FW "$line\n"; } } if ($line =~ /<\/WNT>/) { foreach my $addline(keys %$HRR_wnt) { print FW "<RESOURCE Id=\"$addline\" $$HRR_wnt{$add +line}</RESOURCE>}\n"; } print FW "$line\n"; $Wkey = undef; } } if ($line =~ /UNX/){$Ukey = $line}; if ($Ukey) { $Flag = undef; $Ukey =~ s/\s*//; if ($Ukey ne '</UNX>') { if ($line =~ /Id="(.*?)".*?>(.*?)</) { my $key_id = $1; my $key_val= $2; if (exists $$HRR_unx{$key_id}) { my $val = $$HRR_unx{$key_id}; $val =~ /.*?>(.*?$)/; $val = $1; $line =~ s/>.*?</>$val</; print FW "$line\n"; delete($$HRR_unx{$key_id}); } else { print FW "$line\n"; } } else { print FW "$line\n"; } } if ($line =~ /<\/UNX>/) { foreach my $addline(keys %$HRR_unx) { print FW "<RESOURCE Id=\"$addline\" $$HRR_unx{$add +line}</RESOURCE>}\n"; } print FW "$line\n"; $Ukey = undef; } } if ($Flag) { print FW "$line\n"; } } } }

In reply to Looking for smart solution by Sun751

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.