$string =~ / \w/; # a space followed by a word character
$string =~ /\s\w/; # any whitespace character followed by a word character
$string =~ /\s\S/; # any whitespace character followed by a non-whitespace character
####
#!/usr/bin/env perl
use 5.010; use strict; use warnings;
my @s = ('John', 'John ', 'John Doe', 'John P. Doe'); # last 2 should match
for (@s){
my @v = split /\s+\b/; # split on whitespace followed by a word boundary
if(@v > 1 ){ # if the split did any splitting
say; # do stuff with the line or elements
}
}
##
##
Rate split and check check and split
split and check 145/s -- -1%
check and split 146/s 1% --
##
##
Rate check and split split and check
check and split 112/s -- -17%
split and check 136/s 21% --
##
##
#!/usr/bin/env perl
use 5.010; use strict; use warnings;
use Benchmark qw(:all);
use Data::Printer;
# my @s = ('John', 'John ', 'John Doe', 'John P. Doe') x 1000; # big array 50% need split
my @s = ('John', 'John Poe', 'John Doe', 'John P. Doe') x 1000; # big array 75% need split
cmpthese( 1000, {
'split and check' => \&one,
'check and split' => \&two,
});
sub one {
for (@s){
my @v = split /\s+\b/; # split on a space followed by a word boundary
if($v[1] ){ # if the split did any splitting
# do stuff with the line or elements
}
}
}
sub two {
for (@s){
if (/\s\b/){ # if the line would be split
my @v = split /\s+\b/; # split it
# do stuff with the line or elements
}
}
}