G'day onlyauto,
Welcome to the monastery.
I see you have a solution to your posted problem.
There's another issue I thought I'd point out.
All those if statements:
-
duplicate a lot of code
-
incur a lot of unnecessary processing
-
are error-prone from a maintenance perspective
-
do not lend themselves to extensibility
Consider writing your code more along these lines:
#!/usr/bin/env perl -l
use strict;
use warnings;
my @data = ('X_12_yzAAA', 'X_34_yzBBB', 'X_56_yzCCC', 'X_78_yzAAA',);
my $re = qr{^.+_(\d+)_.+([A-C]{3})$};
my %despatch = (
AAA => sub { print 'found1 with digits: ', shift },
BBB => sub { print 'found2 with digits: ', shift },
CCC => sub { print 'found3 with digits: ', shift },
);
/$re/ && $despatch{$2}->($1) for @data;
Output:
found1 with digits: 12
found2 with digits: 34
found3 with digits: 56
found1 with digits: 78
Now you have:
-
a single regex, only one thing to possibly change here
-
no unnecessary processing: all done in a single statement
-
less chance of maintenance errors because there's a lot less code
-
an easily extensible solution: just add 'DDD => sub { ... }' (or whatever) to %despatch
[Update: and change A-C to A-D in one place.]
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.