Two perl-specific postings on Codeaholic

Discussion in 'Perl Misc' started by axtens, Mar 4, 2008.

  1. axtens

    axtens Guest

    axtens, Mar 4, 2008
    #1
    1. Advertising

  2. axtens wrote:
    > G'day everyone
    >
    > 1. [Perl/PDK/PerlCtrl] Returning an array of arrays for VB6/VBScript
    > http://codeaholic.blogspot.com/2008/02/perlpdkperlctrl-returning-array-of.html
    > 2. [Perl] How not to do it? http://codeaholic.blogspot.com/2008/03/perl-how-not-to-do-it.html


    > (I'm no guru when it comes to Perl, so if you see something that could
    > be expressed in a more efficient manner, please let me know.)


    > 1. Remove all 's'
    > sub rule1 {
    > my $arg = shift;
    > $arg =~ s/s//g;
    > return $arg;
    > }


    sub rule1 {
    ( my $arg = shift ) =~ tr/s//d;
    return $arg;
    }

    > 2. Sort the characters of the word into alphabetic order
    > sub rule2 {
    > my $arg = shift;
    > my @arr = split( //, $arg );
    > @arr = sort @arr ;
    > $arg = join( '', @arr );
    > return $arg;
    > }


    sub rule2 {
    return join '', sort split //, shift;
    }

    > 3. Convert all vowels to 'e
    > sub rule3 {
    > my $arg = shift;
    > $arg =~ s/[a|e|i|o|u]/e/g;
    > return $arg;
    > }


    Why are you converting the '|' character and the 'e' character to 'e'?

    sub rule3 {
    ( my $arg = shift ) =~ tr/aiou/e/;
    return $arg;
    }

    > 4. Replace the first letter with 'n'
    > sub rule4 {
    > my $arg = shift;
    > $arg =~ s/^./n/;
    > return $arg;
    > }


    The . character class matches a lot more than just letters, or did you
    really mean "replace any first character except newline with 'n'".

    sub rule4 {
    ( my $arg = shift ) =~ s/\A[[:alpha:]]/n/;
    return $arg;
    }

    > 5. Drop the last letter
    > sub rule5 {
    > $arg = shift;
    > $arg =~ s/.$//;
    > return $arg;
    > }


    The . character class matches a lot more than just letters.

    sub rule5 {
    ( my $arg = shift ) =~ s/[[:alpha:]]\z//;
    return $arg;
    }

    > 6. Replace letter pairs with 'ow'
    > sub rule6 {
    > $arg = shift;
    > @arr = split( //, "abcdefghijklmnopqrstuvwxyz" );
    > for $letter ( @arr ) {
    > $arg =~ s/$letter$letter/"ow"/eg;
    > }
    > return $arg;
    > }


    sub rule6 {
    ( my $arg = shift ) =~ s/([[:lower:]])\1/ow/g;
    return $arg;
    }

    > Each rule is evaluated against the passed in value in $arg, and stored
    > in $res. $reason is cleared and each test applied to $res.
    >
    > $res = eval( "rule$j(\"$arg\")" );


    Ouch! Use a dispatch table instead of string eval().

    my %rule = (
    1 => sub {
    ( my $arg = shift ) =~ tr/s//d;
    return $arg;
    },
    2 => sub {
    return join '', sort split //, shift;
    },
    3 => sub {
    ( my $arg = shift ) =~ tr/aiou/e/;
    return $arg;
    },
    # more rules here
    );

    # and later on, use rules
    $res = $rule{ $j }( $arg );




    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Mar 4, 2008
    #2
    1. Advertising

  3. On Tue, 04 Mar 2008 06:40:27 GMT, "John W. Krahn"
    <> wrote:

    >Ouch! Use a dispatch table instead of string eval().
    >
    >my %rule = (
    > 1 => sub {
    > ( my $arg = shift ) =~ tr/s//d;
    > return $arg;
    > },
    > 2 => sub {
    > return join '', sort split //, shift;
    > },


    Since the keys are numbers, an array may be appropriate.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Mar 4, 2008
    #3
  4. axtens

    axtens Guest

    On Mar 4, 11:44 pm, Michele Dondi <> wrote:
    > On Tue, 04 Mar 2008 06:40:27 GMT, "John W. Krahn"
    >
    > <> wrote:
    > >Ouch! Use a dispatch table instead of string eval().

    >
    > >my %rule = (
    > > 1 => sub {
    > > ( my $arg = shift ) =~ tr/s//d;
    > > return $arg;
    > > },
    > > 2 => sub {
    > > return join '', sort split //, shift;
    > > },

    >
    > Since the keys are numbers, an array may be appropriate.
    >
    > Michele
    > --
    > {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    > (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    > .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    > 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


    Wow, this is great stuff. Thanks, both of you! Shall cook up another
    posting during the week and put your names in lights (unless you'd
    prefer otherwise, in which case email me off-list).

    Kind regards,
    Bruce.
    axtens, Mar 9, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allan Herriman

    Re: Function postings for VHDL

    Allan Herriman, Jul 25, 2003, in forum: VHDL
    Replies:
    1
    Views:
    2,191
    Willem Oosthuizen
    Jul 25, 2003
  2. Richard B. Katz

    MAPLD 2005 Postings On-line

    Richard B. Katz, Sep 24, 2005, in forum: VHDL
    Replies:
    1
    Views:
    454
    Bill McCulley
    Sep 27, 2005
  3. Claes Rådström

    Why multiple postings ?

    Claes Rådström, Feb 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    316
    Claes Rådström
    Feb 18, 2004
  4. Jan Faerber
    Replies:
    16
    Views:
    589
    Jan Faerber
    Jan 23, 2005
  5. Edwin van der Vaart

    ot: multi-postings

    Edwin van der Vaart, Aug 14, 2005, in forum: HTML
    Replies:
    4
    Views:
    373
    Edwin van der Vaart
    Aug 15, 2005
Loading...

Share This Page