in reply to Counting XML Blocks in XML file and Calculating Total Amount

Others have given you advice how to improve your code if you do it the regex way, here's how you could use existing XML processing tools if your input were valid XML (which it isn't in your example).
#!/usr/bin/perl use strict; use warnings; use XML::Twig; print "This will calculate the total amount and To no of transactions +\n"; my $totalamt = 0; my $cnt = 0; my $twig = XML::Twig->new( twig_handlers => { b => \&b_handler, payment_amount => \&amt_handler }); $twig->parse(do {local $/; <DATA>}); print "The total amount found is $totalamt \n"; print "Total Transactions are $cnt \n"; $twig->purge(); sub b_handler { my ($t, $b_elem) = @_; $cnt++; $b_elem->delete(); } sub amt_handler { my ($t, $amt_elem) = @_; $totalamt += int($amt_elem->text()); } __DATA__ <a> <b> <client_account_cred>68789790390909090489</client_acco +unt_cred> <Payment_UTR_No>MTRIN10909890896</Payment_UTR_No> <payment_amount>700000</payment_amount> </b> <b> <client_account_cred>9033753053985392INR</client_accou +nt_cred> <Payment_UTR_No>938573895735154</Payment_UTR_No> <payment_amount>1222706</payment_amount> </b> <b> <client_account_cred>9284723472047222INR</client_accou +nt_cred> <Payment_UTR_No>RP JLLKL7687</Payment_UTR_No> <payment_amount>1437865.95</payment_amount> </b> </a>