Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Check for string in array and substitue

by sandy1028 (Sexton)
on Jun 04, 2009 at 08:54 UTC ( [id://768355]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Check for string in array and substitue
by Corion (Patriarch) on Jun 04, 2009 at 09:08 UTC

    I consider it very rude that you open a new thread without even mentioning that you have four other threads already, all going around the same problem, where you already got lots of helpful answers.

    Maybe you can take a step back and explain what problems you have with the answers you got there and how they did not work for you.

    Also, as you have been told numerous times already, it would be very helpful for us to help you better if you showed example input and output, and also identified exactly how the output was not what you expected.

    Your program initializes an array @entites, but you don't use that array anywhere. Please explain in simple text, not in a Perl program, what should be done.

    Also, as you have been told several times already, please put all your output in between <code>...</code> tags, so we can see the difference between & and &amp;. If you reread your post, you will notice that & and & look quite the same unless you write them as <code>&</code> and <code>&amp;</code>.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Check for string in array and substitue
by almut (Canon) on Jun 04, 2009 at 09:23 UTC

    You could try something like this, i.e. put your not-to-be-replaced entites into a hash for easy lookup:

    #!/usr/bin/perl my %entities = map { $_ => 1 } qw(&amp; &quot; &lt; &gt; &nbsp; &copy; +); while (my $line = <DATA>) { $line =~ s/(&(\w+?;)?)/exists $entities{$1} ? $1 : "&amp;$2"/eg; print $line; } __DATA__ foo &amp; &quot; bar &blah; &foo baz & ... TEST&TEST;A&E&an &nbsp; HTML--- &nbsp; string - &lt; &copy; TVS&gt;

    Would output:

    foo &amp; &quot; bar &amp;blah; &amp;foo baz &amp; ... TEST&amp;TEST;A&amp;E&amp;an &nbsp; HTML--- &nbsp; string - &lt; &copy +; TVS&gt;

    (note that if you run this under strictures, it'll complain "Use of uninitialized value in concatenation" in case $2 is empty... I'll leave this as an exercise for you to fix :)

    Update: I hadn't followed your other thread... so looking at wfsp's solution there, I'd say just use that instead and be happy :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-24 12:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found