counting word occurances

V

vali

A. Sinan Unur said:
Thank you for the correction. I failed to notice the typo in vali's post.

Just followed up on Jurgen's one.
I was suprised to see just how much more efficient \s+ was compared to \s*.

That's what I thought when using it on my approach.
Finally, this supports my assertion that s/(^\s+|\s+$)//g; is not the same
as what the answer to the FAQ recommends.

That's right. I'll start using it (and check more often the FAQs).

Thanks (to all of you) for this (and other) insights !

__Vali
#! /usr/bin/perl

use strict;
use warnings;

use Benchmark ':all';

my $INPUT = [
'pear ',
' apple ',
'apple',
' orange ',
' mango ',
'mango',
' pear',
' cherry ',
'apple',
'',
];

sub s1 {
my @input = @{ $INPUT };

for (@input) {
s/^\s*//;
s/\s*$//;
}
}

sub faq {
my @input = @{ $INPUT };

for (@input) {
s/^\s+//;
s/\s+$//;
}
}

cmpthese 0, {
s1 => \&s1,
faq => \&faq,
};

__END__

D:\Home>perl t.pl
Rate s1 faq
s1 8638/s -- -25%
faq 11489/s 33% --

Sinan
 
A

A. Sinan Unur

[ Please quote properly. You inserted your responses in the middle of
my responses, and did not delete parts you were not replying to. This
makes it difficult to carry on an intelligent conversation with you.
]
Just followed up on Jurgen's one.

So what?
That's what I thought when using it on my approach.

Is that why you captured unnecessarily, alternated unnecessarily, used g
unnecessarily etc???
That's right. I'll start using it (and check more often the FAQs).

You'll start using what?

Sinan
 
V

vali

A. Sinan Unur said:
[ Please quote properly. You inserted your responses in the middle of
my responses, and did not delete parts you were not replying to. This
makes it difficult to carry on an intelligent conversation with you.
]

Point taken.

Nothing. Or maybe another way of saying "Thank you for the correction. I
failed to notice the typo in Jurgen's post"
Is that why you captured unnecessarily, alternated unnecessarily, used g
unnecessarily etc???

Nope. That (to be more precise: s/(^\s+|\s+$)//g;) was just a poor
attempt to avoid two steps when strip any beginning/end space(s) from a
string.
^^


You'll start using what?

For those that couldn't get it from the context I was referring to the
FAQ recommendation on this regard which, to be more specific, is:
for ($string) {
s/^\s+//;
s/\s+$//;
}

Have a good and prolific day !

__Vali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top