Hi Jeff 'japhy' Pinyan, you wrote:
On May 18, Dale said:
Hi John W. Krahn, you wrote:
If I understand you correctly then this will do what you want:
$str =~ tr/ //s;
Or if you want a slower method:
$str =~ s/ +/ /g;
This might sound a strange question, but why is the first one faster?
The first one is not a regex, it's merely a character transliteration.
I'll take your word for it! :)
$str = join ' ', split ' ', $str;
This just confuses me!
split(' ', $str) returns a list of chunks of non-whitespace, and join(' ') joins those chunks together with one space.
OK - that makes sense now!
I thought that I could remove lines from the data by matching whether the first character was a letter by using the 'if' statement as :
if(substr($line,0,1) eq [a-zA-Z])
...but this doesn't work. Neither does :
if($line eq [\s\d])
...to try and find if it's just contain numbers and spaces.
They don't work because eq is for strings, and you're trying to use character classes (or more generally, regexes).
if (substr($line, 0, 1) =~ /[a-zA-Z]/) { ... }
and
if ($line =~ /^[\s\d]+$/) { ... }
probably do what you want.
So when ever I want to use a function like this, I need to use =~. Makes sense now - I didn't think of using it as I was stuck in the mindset of using eq.
Thanks for the advice Jeff! -- Dale
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>