Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Chaining string ops

by davido (Cardinal)
on Aug 28, 2003 at 17:39 UTC ( [id://287462]=note: print w/replies, xml ) Need Help??


in reply to Chaining string ops

I'll present two methods which work fairly well. The first method is cleaerer to read, but if the list of substitutions is long, it can lead to excessive typing.

The first method favors long lists of strings to undergo the same substitutions. For example, if $str were actually a large array called @str, you would probably favor the first method.

The second method favors lots of substitutions to be performed for a single string.

my $str = "the boy walked the dog"; for ( $str ) { s/walked/fed/; s/boy/girl/; s/dog/Audrey II/; }

This method just relies on the fact that the for statement causes $_ to alias $str within the scope of the for statement. And regexp binding (=~) binds to $_ if there is no other variable specified.

You could also do it this way:

my $str = "the boy walked the dog"; my %subs = ( "walked", "fed", "boy", "girl", "dog", "Audrey II" ); foreach ( keys %subs ) { $str =~ s/$_/$subs{$_}/; }

This method is pretty much opposite of my first example. But it can be handy if you have a lot of substitutions.

Dave

"If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Replies are listed 'Best First'.
Re^2: Chaining string ops
by Aristotle (Chancellor) on Aug 28, 2003 at 18:08 UTC
    You can get rid of the loop in your second example entirely.
    my %subst_for = ( "walked" => "fed", "boy" => "girl", "dog" => "Audrey II" ); $_ = "the boy walked the dog"; my $alt = join '|', map quotemeta, keys %subs; s/($alt)/$subst_for{$1}/g;

    Makeshifts last the longest.

      Yes, you can do that, which creates a potentially huge alternation list for the regexp engine to swollow. I'm not sure that would be more efficient though, neither is it as clear.

      Alternation is a convenient tool inside a regexp, and I'm glad to see an example of how to make it work in the context of substituting multiple items with multiple substitute items.

      But alternation is also very inefficient, and can lead to a lot of backtracking even in relatively simple regular expressions. In that context, a loop may be more time efficient. Only benchmarking could say for sure.

      Dave

      "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

        Yes, you can do that, which creates a potentially huge alternation list for the regexp engine to swollow. I'm not sure that would be more efficient though, neither is it as clear.

        Regex::PreSuf to the rescue!! It'll fix your efficiency problem, although it won't do much for readability :)
        --
        Mike

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://287462]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found