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

Pattern Matching

by Zo (Scribe)
on Dec 16, 2002 at 15:52 UTC ( #220248=perlquestion: print w/replies, xml ) Need Help??

Zo has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone,
I have code that works, but I was wondering if the following could be done in one single statement? I'm sure it could but my perl experience isn't so advanced yet to make a single working statement out of all of this. I've hit the books literally and tried different sequences and attempts, but none worked. Any thoughts? I'm sure a few of you could have fun with this.
$report; $report=~s/[0-9]//g; # filter out any numbers $report=~s/_//g; # filter out any underscores $report=~s/CRS//g; # filter out the phrase 'CRS' if caps $report=~s/crs//g; # filter out the phrase 'crs' if lower $report=~s/-//g; # filter out dashes $report=~s/html//g; # filter out the phrase 'html' $report=~s/\.//g; # filter out decimal points print $report;

Thank you all in advance for any help.

Replies are listed 'Best First'.
(jeffa) Re: Pattern Matching
by jeffa (Bishop) on Dec 16, 2002 at 16:01 UTC
    I actually prefer it the way it is. It should be fairly obvious to non-Perl programmers what is going on here, and slamming everything into one line will certainly make some ears blead. However, by grouping things together, you can have a little of both worlds:
    $report =~ tr/0-9//d; # numbers $report =~ tr/-_.//d; # underscores, dashes, and periods $report =~ s/crs//ig; # crs, lower or upper $report =~ s/html//g; # filter out the phrase 'html'

    Nice catch merlyn. I'll leave that comment untouched, as i had originally tried /_-./ before posting. I should add that i rarely use comments like that anymore as they are maintenance nuisances, IMHO.


    (the triplet paradiddle with high-hat)
      $report =~ tr/-_.//d; # underscores, dashes, and periods
      I would yellow-flag that in a code review, since the comment does not precisely match the action, and needlessly. Some maintenance programmer is going to wonder "Why does he call a dash an underscore?".

      I might also add a comment to explain why it's not

      which would invoke the range-ness of the dash. Or maybe I'd change it to:
      to make it clear that the dash doesn't have its dashing nature.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

      In the line

      $report =~ tr/-_.//d; # underscores, dashes, and periods

      won't the . match any character and filter it out? Don't you need to escape it, or does it work differently with the tr as opposed to s?

      There is no emoticon for what I'm feeling now.

        No, tr/// is not s/// or m//, but it's y/// :-)

        The transliteration operator does not have anything to do with regular expressions, that's why the . doesn't have special meaning in it. See perlop.

      Of course that can be condensed a bit:
      $report =~ tr/\-_.0-9//d; $report =~ s/$_//ig for qw(crs html);
      I debated putting the crs and html together in an alternation but I'm relatively certain it'd be a loss so I picked a for to satisfy the once and only once rule.

      Makeshifts last the longest.

      You might want to ignore the case with the last substitution as well... many people write it HTML. (:
Re: Pattern Matching
by gjb (Vicar) on Dec 16, 2002 at 22:16 UTC

    I think you may want to change the order of those statements since weird things could happen as it is now.

    Since you're first taking out digits and underscores, you might be artificially creating a 'csr' phrase. Take 'doc09_srt_whatever', do you want it to end up as 'dotwhatever'?

    Of course this may never happen with your specific data (now?), but it's something to keep in mind.

    Hope this helps, -gjb-

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://220248]
Approved by Tanalis
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2019-05-27 14:32 GMT
Find Nodes?
    Voting Booth?
    Do you enjoy 3D movies?

    Results (156 votes). Check out past polls.