Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Manipulating a string in a tagged file

by rsriram (Hermit)
on Mar 12, 2007 at 10:51 UTC ( [id://604313]=perlquestion: print w/replies, xml ) Need Help??

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

Hello all,

I have a tagged text file as below:

<tag1>Complete</tag1><tag2>3386</tag2><tag3>77844</tag3><tag4>11</tag4><tag5>30</tag5><tag6>4.7</tag6>

If the content of <tag1> is "Complete", the content of <tag6> should be multiplied by 100. I tried the following code but did not work. Can someone help me on this?

if($_ =~ /<tag1>Complete<\/tag1>/) { $_ =~ s/<tag6>(.+?)<\/tag6>/<tag6>$1 * 100<\/tag6>; }

Replies are listed 'Best First'.
Re: Manipulating a string in a tagged file
by GrandFather (Saint) on Mar 12, 2007 at 11:07 UTC

    You have a couple of bugs in your regex. You are missing the closing '/'.

    You need an e flag to evaluate the substitution expression. However as it stands that gets messy because of the mixture of string litteral characters and characters that represent a calculation. No way Perl can disambiguate that!

    I'd be inclined to do it like this:

    use strict; use warnings; my $str = "<tag1>Complete</tag1><tag2>3386</tag2><tag3>77844</tag3><ta +g4>11</tag4><tag5>30</tag5><tag6>4.7</tag6>"; if ($str =~ m|<tag1>Complete</tag1>|) { $str =~ s|(?<=<tag6>)(.*?)(?=</tag6>)|$1 * 100|e; } print $str;

    Prints:

    <tag1>Complete</tag1><tag2>3386</tag2><tag3>77844</tag3><tag4>11</tag4 +><tag5>30</tag5><tag6>470</tag6>

    Note the use of strictures (use strict; use warnings;). The regex uses a look behind and a look ahead assertion to anchor the capture group so that the replaced text is only the number. The /e then gets the calculation done in the substitution.


    DWIM is Perl's answer to Gödel
Re: Manipulating a string in a tagged file
by Samy_rio (Vicar) on Mar 12, 2007 at 11:00 UTC

    Hi rsriram, try like this,

    use strict; use warnings; my $str = '<tag1>Complete</tag1><tag2>3386</tag2><tag3>77844</tag3><ta +g4>11</tag4><tag5>30</tag5><tag6>4.7</tag6>'; if($str =~ /<tag1>Complete<\/tag1>/i) { $str =~ s/<tag6>(.+?)<\/tag6>/"<tag6>".( $1 * 100 )."<\/tag6>"/ei; } print $str;

    Updated

    Typo error in your replacement statement. Use 'e' option modifier for execute the statement in the replacement part.

    See the documentation for further clarification perlre

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Manipulating a string in a tagged file
by ides (Deacon) on Mar 12, 2007 at 22:56 UTC

    This looks like it might be easier to do with something like XML::Simple and manipulate the data directly and rewrite it out to disk. Just an idea....

    Frank Wiles <frank@revsys.com>
    www.revsys.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://604313]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-28 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found