Executing stored substitutions...

Discussion in 'Perl Misc' started by gibbering poster, Jan 22, 2004.

  1. Suppose I has some coderefs in an array, and wanted to loop through them
    and apply them... more specifically to a string:

    my @subs = (
    { s/\s+//g }, { s/-/ /g },
    );

    my $string = "\tJoe-Smith ";

    for (@subs) {
    local *_ = $string;
    do &{$_}; # I know this is horribly wrong
    }

    print $string; # Want this to print "Joe Smith"


    Can someone please show me the way here? Sorry in advance if I missed a
    perldoc.

    Thanks!

    PS ... Is there a way to catch the return value of those substitions
    (i.e. their success or failure)?
     
    gibbering poster, Jan 22, 2004
    #1
    1. Advertising

  2. gibbering poster wrote:
    > Suppose I has some coderefs in an array, and wanted to loop
    > through them and apply them... more specifically to a string:
    >
    > my @subs = (
    > { s/\s+//g }, { s/-/ /g },


    You need to say that they are code refs:

    sub { s/\s+//g }, sub { s/-/ /g },

    > );
    >
    > my $string = "\tJoe-Smith ";
    >
    > for (@subs) {
    > local *_ = $string;
    > do &{$_}; # I know this is horribly wrong
    > }


    You may want to try this:

    for my $sub (@subs) {
    local $_ = $string;
    do &{$sub};
    $string = $_;
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 22, 2004
    #2
    1. Advertising

  3. gibbering poster <> wrote:

    > Suppose I has some coderefs in an array,


    > my @subs = (
    > { s/\s+//g }, { s/-/ /g },
    > );



    There are no coderefs in that array you know.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 22, 2004
    #3
  4. gibbering poster

    ko Guest

    "gibbering poster" <> wrote in message news:<JwEPb.14542$>...
    > Suppose I has some coderefs in an array, and wanted to loop through them
    > and apply them... more specifically to a string:
    >
    > my @subs = (
    > { s/\s+//g }, { s/-/ /g },
    > );


    Those are (empty) hashrefs...

    > my $string = "\tJoe-Smith ";
    >
    > for (@subs) {
    > local *_ = $string;
    > do &{$_}; # I know this is horribly wrong
    > }
    >
    > print $string; # Want this to print "Joe Smith"
    >
    > Can someone please show me the way here? Sorry in advance if I missed a
    > perldoc.
    >
    > Thanks!


    One way to do what you had in mind:

    my @subs = (
    sub { $_[0] =~ s#\s+##g },
    sub { $_[0] =~ s#-# #g },
    );

    foreach ( @subs ) {
    $_->($string);
    }
    print "$string\n";

    But really, if you want to apply *all* the substitutions on the
    string(s), put all of them in one sub...

    If, on the other hand, you want to *selectively* perform the
    substitutions (or run whatever code you like) use a dispatch table:

    my $dt = {
    whitespace => sub { $_[0] =~ s#\s+##g },
    dashes => sub { $_[0] =~ s#-# #g },
    # other coderefs...
    };

    foreach my $hrkey( qw[whitespace dashes] ) {
    $dt->{$hrkey}->($string);
    }
    print "$string\n";

    The following documentation are good reading in this case:

    perlreftut
    perldsc
    perllol

    > PS ... Is there a way to catch the return value of those substitions
    > (i.e. their success or failure)?


    From perlop:

    s/PATTERN/REPLACEMENT/egimosx

    Searches a string for a pattern, and if found, replaces that pattern
    with the replacement text and returns the number of substitutions
    made.

    HTH - keith
     
    ko, Jan 22, 2004
    #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. RJGraham
    Replies:
    6
    Views:
    486
    RJGraham
    Jun 28, 2004
  2. Chris Goller
    Replies:
    4
    Views:
    380
    Jordan Stewart
    Mar 9, 2005
  3. Tony Eva
    Replies:
    1
    Views:
    491
    Jeff Epler
    Nov 9, 2003
  4. Vivien Mallet

    Backslash substitutions

    Vivien Mallet, Sep 29, 2004, in forum: Python
    Replies:
    1
    Views:
    314
    Peter Otten
    Sep 29, 2004
  5. rh0dium
    Replies:
    6
    Views:
    273
    Dennis Lee Bieber
    Aug 5, 2005
Loading...

Share This Page