Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

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

Another possibility:

>perl -wMstrict -le "for my $s (qw( B111 B111B111 B111B111B111 A111 A111A111 A111A111A111 A111B111 A111B111A111 A111B111A111B111 A111B111A111B111A111 B111A111 B111A111B111 B111A111B111A111 B111A111B111A111B111 B BB BBB A AA AAA BA BAB AB ABA xxx xx x), '', ) { local our $y_n; ()= $s =~ m{ ([AB]) (?{ $y_n = $^N eq 'A' ? 'yes' : 'no' }) }xmsg; if (defined $y_n) { printf qq{%3s: '%s' \n}, $y_n, $s; } else { warn qq{no A or B found in '$s'}; } } " no: 'B111' no: 'B111B111' no: 'B111B111B111' yes: 'A111' yes: 'A111A111' yes: 'A111A111A111' no: 'A111B111' yes: 'A111B111A111' no: 'A111B111A111B111' yes: 'A111B111A111B111A111' yes: 'B111A111' no: 'B111A111B111' yes: 'B111A111B111A111' no: 'B111A111B111A111B111' no: 'B' no: 'BB' no: 'BBB' yes: 'A' yes: 'AA' yes: 'AAA' yes: 'BA' no: 'BAB' no: 'AB' yes: 'ABA' no A or B found in 'xxx' at -e line 1. no A or B found in 'xx' at -e line 1. no A or B found in 'x' at -e line 1. no A or B found in '' at -e line 1.

Update: And allowing for the possibility of side-effects (e.g., the values in the  %xlat hash could be anonymous subroutine references) (note:  //p and  ${^MATCH} only available in Perl 5.10+):

>perl -wMstrict -le "my %xlat = qw(A yes B no); ;; my $set = join '', map quotemeta, keys %xlat; $set = qr{ [$set] }xms; ;; my $xlat_keys = join ' or ', keys %xlat; ;; for my $s (qw( B111 B111B111 B111B111B111 A111 A111A111 A111A111A111 A111B111 A111B111A111 A111B111A111B111 A111B111A111B111A111 B111A111 B111A111B111 B111A111B111A111 B111A111B111A111B111 B BB BBB A AA AAA BA BAB AB ABA xxx xx x), '', ) { local our $y_n; use re 'eval'; ()= $s =~ m{ $set (?{ $y_n = $xlat{${^MATCH}} }) }xmspg; if (defined $y_n) { printf qq{%3s: '%s' \n}, $y_n, $s; } else { warn qq{no $xlat_keys found in '$s'}; } } " no: 'B111' no: 'B111B111' no: 'B111B111B111' yes: 'A111' yes: 'A111A111' yes: 'A111A111A111' no: 'A111B111' yes: 'A111B111A111' no: 'A111B111A111B111' yes: 'A111B111A111B111A111' yes: 'B111A111' no: 'B111A111B111' yes: 'B111A111B111A111' no: 'B111A111B111A111B111' no: 'B' no: 'BB' no: 'BBB' yes: 'A' yes: 'AA' yes: 'AAA' yes: 'BA' no: 'BAB' no: 'AB' yes: 'ABA' no A or B found in 'xxx' at -e line 1. no A or B found in 'xx' at -e line 1. no A or B found in 'x' at -e line 1. no A or B found in '' at -e line 1.

In reply to Re: Change variable multiple times within a single string? by AnomalousMonk
in thread Change variable multiple times within a single string? by JTomb

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 admiring the Monastery: (4)
As of 2024-04-26 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found