Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: tr operator in eval -- updated

by AnomalousMonk (Archbishop)
on Sep 26, 2020 at 16:43 UTC ( [id://11122241]=note: print w/replies, xml ) Need Help??


in reply to tr operator in eval -- updated

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: tr operator in eval -- updated
by pgmer6809 (Sexton) on Sep 27, 2020 at 02:57 UTC
    Thanks very much. There is a lot I don't know about perl, and even more so about 'modern' perl since I only dive into programming about once per year. I had in fact discovered the s/char//g trick after making the OP. Those 'set' operations look very useful. I will have to study up on them. In this case I am not (yet) sure they will work, since a suit like "AKQxxx" is valid but one like "AAKxxx" is not. (There is only one Ace of spades for example.) So assuming counting members of the set AKQ would return 3 in both cases(??), but the second is invalid. But no doubt I will find a use for set operations now that I know they are available. pgmer6809 PS. I have 'discovered' (I am sure I am not the first!) that a way to avoid destruction of the original string is to use $& as in $deal =~ s{AKQJ}{$&}xg; will count the number of top cards in $deal, but leave it unchanged: </code> $deal="AQTxx"; $c = $deal =~ s{AKQJ}{$&}xg; print "$c,$deal\n"; output: 2,AQTxx
      Those 'set' operations ...

      Please note that the term "set" is used very loosely in this post. :)

      ... a suit like "AKQxxx" is valid but one like "AAKxxx" is not. (There is only one Ace of spades ....)

      This seems like a two-phase problem. Once you verify that a string is in a standard format with, say, no character occuring twice, it may be very easy to count characters unambiguously.

      Within reasonable limits, a single regex could be very handy for verifying that a string:

      • has min to max characters from a given set of characters;
      • that the characters in the string are only from the set;
      • that each character from the set occurs only once in the string; and
      • that the characters in the string are in a required order.

      It may be enough just to verify that no character occurs more than once in a string. For that,
          length $string == uniq split '', $string
      or
          $string !~ m{ (.) (?= .*? \1) }xms
      does the trick.

      And there are many other conceivable approaches, all depending on your precise requirements.

      Update: Added another simple no-repeat test.


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found