Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

You will be much better suited by using a proper parser than hoping to do the parsing through a regular expression.

That said, it seems to me that you only want to replace variable names and not function names, so, barring any special/convenient notation for your variable names, I can only suggest that you build a proper regular expression out of your variable names:

my $variables = join "|", map { "\\b$_\\b" } reverse sort keys %cats; $variables = qr/$variables/; $texttosub =~ s|($variables)|$cats{ $1 }|g; # now there are no more variables, so we can evaluate the thing

You have two logic errors in your code:

While your string still contains variable names, it makes no sense to try to evaluate it.

Your code assumes that variable names should be matched independent of case, but your replacement can only replace the case that was stored in %cats. Your code will never replace blackCat with the value for blackcat.




  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found