Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Regexp string concat

by delirium (Chaplain)
on Jun 14, 2004 at 21:32 UTC ( [id://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 (Patriarch) 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found