Try this:
Win8 Strawberry 5.30.3.1 (64) Sat 09/26/2020 0:05:59
C:\@Work\Perl\monks
>perl
use strict;
use warnings;
my $kards;
my $bad = 'AKQJT98765432KKKK';
my $card = 'K';
eval "\$kards = \$bad =~ tr/$card//";
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__
1: Num of 'K' in AKQJT98765432KKKK is == 5
2: Num of 'K' in AKQJT98765432KKKK is == 5
Note that \$kards \$bad in the eval string are
escaped (update: and $card is not). Why?
Update: Just read your update to the OP and I
think you pretty much have the idea I was trying to get across.
However, now try running the OP update code with strict and
warnings enabled.
Give a man a fish: <%-{-{-{-<
-
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.
|