Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Perl Golf (was RE: (Ovid) RE: Pig Latin)

by japhy (Canon)
on Jul 23, 2000 at 23:12 UTC ( #23996=note: print w/replies, xml ) Need Help??

in reply to (Ovid) RE: Pig Latin
in thread Pig Latin

I'm just playing through. I made your code case-insensitive and did a bit of regex tomfoolery.
# updated (5 years later!) s/\b(qu|[^\W0-9aeiou_]+)?([a-z]+)/$1?"$2$1ay":"$2way"/ieg;
I don't see the need to save 3 pieces of data. And using [^\W0-9_] is shorter than [] and [b-df-hj-np-tv-z] and it forces the reader to think for a second. ;). And I saved space with the quoting on the RHS.

Score: 53.


Replies are listed 'Best First'.
(Ovid) RE: Perl Golf
by Ovid (Cardinal) on Jul 23, 2000 at 23:49 UTC
    japhy, originally, I was constructing a rather longer and optimized script to do the pig latin conversion. Then I went back and reread vroom's specs. First, I didn't use the /i modifier because he said we were to assume the data was lowercase and he wanted the shortest possible code.

    The reason I am using three backreferences is because the data saved to $2 is tricky. Your equivalent (ignoring the "qu" problem) is [^\W0-9_]. This allows you to match all alphabeticals but does no discrimination for vowels. However, you apeared to notice this when you mentioned [b-df-hj-np-tv-z]. Therefore, I suspect that you intended the following and (assuming you did intend this) I offer you kudos for a clever regex:

    I also noticed that, in this case, using the /i modifier ignored vroom's "lowercase" spec, but does result in a shorter regex.


      Oh, d'oh, I'm silly. I meant to add 'aeiou' to the character class, I really did, since that was the whole reason I introduced it. :) And I'm sorry I hadn't checked vroom's specs.

      By the way, since Pig Latin does not produce a 1-to-1 mapping of normal strings to PL-strings, you can't reasonably reverse this process. Example: flea and leaf both go to eaflay.

        "does not produce a 1-to-1 mapping"

        As children, my brothers and I used a dialect of Pig Latin that did provide 1-to-1 mapping.   If I remember right, flea translated to lea-fay, yet leaf translated to eaf-lay.

        Off the top of my head, TH was the only consonant combination that didn't break like that.   How might such discernment be added to y'all's way-clever regexes?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://23996]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-06-19 21:00 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.