Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

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

When using an eval STRING; statement, I find it very helpful to build the string separately so that it can be examined with the Perl debugger (see perldebug) or even with a simple print point.

Win8 Strawberry 5.30.3.1 (64) Sat 09/26/2020 11:37:04 C:\@Work\Perl\monks >perl use strict; use warnings; my $kards; my $bad = 'AKQJT98765432KKKK'; my $card = 'K'; my $e_string = "$kards = $bad =~ tr/$card//"; # print "e_string: >$e_string< \n"; # for debug eval $e_string; print "1: Num of '$card' in $bad is == $kards \n"; my $kings = $bad =~ tr/K//; print "2: Num of '$card' in $bad is == $kings \n"; __END__ Use of uninitialized value $kards in concatenation (.) or string at - +line 8. Use of uninitialized value $kards in concatenation (.) or string at - +line 12. 1: Num of 'K' in AKQJT98765432KKKK is == 2: Num of 'K' in AKQJT98765432KKKK is == 5
Uncomment the print point and the problem with the eval becomes obvious. (Enabling warnings helps too!)

On another topic...

You seem to have been going over code examples as a self-teaching exercise. That's commendable, but it should be pointed out that in practice, there are arguably better ways of handling the "count the occurrences of an arbitrary set of characters in a string" problem. eval is expensive, and it's better to avoid the guillotine when a scalpel will do. A couple of examples (of, I'm sure, many).

Win8 Strawberry 5.30.3.1 (64) Sat 09/26/2020 12:14:28 C:\@Work\Perl\monks >perl use strict; use warnings; my $n_found; my $string = 'AKQJT98765432KKKK'; my $set = 'AQK'; # s/// probably fastest in practice. my $destroy = $string; # s/// alters its target $n_found = $destroy =~ s{ [\Q$set\E] }{}xmsg; print "s///: Num of '$set' in '$string' == $n_found \n"; # m// probably a bit slower, use of =()= a bit tricky. $n_found =()= $string =~ m{ [\Q$set\E] }xmsg; print "m//: Num of '$set' in '$string' == $n_found \n"; # tr/// is fastest, but needs expensive eval for arbitrary sets. $n_found = $string =~ tr/KAQ//; print "tr///: Num of 'KAQ' in '$string' == $n_found \n"; __END__ s///: Num of 'AQK' in 'AKQJT98765432KKKK' == 7 m//: Num of 'AQK' in 'AKQJT98765432KKKK' == 7 tr///: Num of 'KAQ' in 'AKQJT98765432KKKK' == 7
This code also works under Perl version 5.8.9.


Give a man a fish:  <%-{-{-{-<


In reply to Re: tr operator in eval -- updated by AnomalousMonk
in thread tr operator in eval -- updated by pgmer6809

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



  • 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 drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-23 19:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found