[ic] perl issue

Christian Brink cbrink at brinkrods.com
Sat Jul 17 17:58:38 EDT 2004


>  This may be better asked in a perl group but I know many perl wizards
>  read this list so thought I'd try. I want to strip all characters from a
>  string that come before the first hyphen, as well as that first hyphen.
>  For example I want

This is tested code:

Example 1:
my $prefix;
if ($string =~ /^([^-]+-)/) {
	$prefix = $1;

Method 2:

my $prefix = join('',(split(/(-)/,$string,2))[0..1]);

Method 3: the last one broken down for clarity;

my $string = 'ABC-DEF-G';

my @tmp = split(/(-)/,$string,2);
# @tmp is ('ABC','-','DEF','-','G')

my $prefix = $tmp[0] . $tmp[1];

I did not benchmark this code, but my guess would be Method 2 would be
quickest, the profiler should be able to optimize it the best.

Christian Brink - Fly Rod Maker
cbrink at brinkrods.com
Authorized Gatti Dealer

More information about the interchange-users mailing list