Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Regexp string concat

by delirium (Chaplain)
on Jun 14, 2004 at 21:32 UTC ( #366699=note: print w/replies, xml ) Need Help??

in reply to Regexp string concat

I think we can solve this without doing any fancy sorting or iterating. How about a simple regular expression approach using upper and lower case characters as our markers?

#!/usr/bin/perl use strict; use warnings; $\ = $/; my $string = 'eichenbaumschule'; my @query = qw(baum ums eic chu le); $string =~ s/$_/uc $_/ieg for @query; @query = $string =~ /([A-Z]+)/g; print for @query; __OUTPUT__ EIC BAUMSCHULE

Replies are listed 'Best First'.
Re^2: Regexp string concat
by BrowserUk (Pope) on Jun 14, 2004 at 23:12 UTC

    This is really neat++.

    As the target is genome work which usually involves large volumes of big strings, using split '[a-z]+', string; to avoid the capture brackets will save a little time.

    sub delirium{ my( $string, $qRef ) = @_; $string =~ tr[A-Z][a-z]; $string =~ s/$_/uc $_/ieg for @$qRef; return reduce{ length $a > length $b ? $a : $b } split '[a-z]+',; }

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      An optimisation I've thought of for this, even though the thread is long dead, is to lc the original string, and have all the strings to match as upper case, then all you need do is: $string =~ s/$_/$_/i for @$qRef; I'm sure the regex is considerably faster for having taken the executable bit out of it.
Re^2: Regexp string concat
by runrig (Abbot) on Jun 14, 2004 at 22:33 UTC
    For a slight speed boost, use compiled regular expressions:
    my $string = 'eichenbaumschule'; my @query = qw(baum ums eic chu le); @query = map { qr/($_)/i } @query; $string =~ s/$_/\U$1/g for @query;
    (I get ~6000 iterations/sec this way vs. ~3000 your way).

    Update: Just realized, this is only a factor if you do the same substitutions multiple times in the same program...if you're only doing this once per run, then you may as well do it delirium's way :-)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-05-27 14:05 GMT
Find Nodes?
    Voting Booth?