How to in perl

B

Bill H

I do a lot of small routines in perl for manipulating data and know
there has to be an easier way of trimming the spaces off the begining
and ending of a variable. I use the following which is basically a
translation of a BASIC routine I have used for years:

while(substr($temp,0,1) eq " ")
{
$temp = substr($temp,1);
}
while(substr($temp,-1) eq " ")
{
$temp = substr($temp,0,length($temp) - 1);
}

There has to be a simpler way in perl than this.

Bill H
 
A

A. Sinan Unur

Subject: How to in perl

Please see

I do a lot of small routines in perl for manipulating data and know
there has to be an easier way of trimming the spaces off the begining
and ending of a variable. I use the following which is basically a
translation of a BASIC routine I have used for years:

while(substr($temp,0,1) eq " ")
{
$temp = substr($temp,1);
}
while(substr($temp,-1) eq " ")
{
$temp = substr($temp,0,length($temp) - 1);
}

#!/usr/bin/perl

use strict;
use warnings;

my $str = q{ sdfkj sdkfj };

$str =~ s/ \A \s+ //x;
$str =~ s/ \s+ \z //x;

print "-$str-\n";

__END__

Sinan
 
M

Matt Garrish

Bill H said:
I do a lot of small routines in perl for manipulating data and know
there has to be an easier way of trimming the spaces off the begining
and ending of a variable. I use the following which is basically a
translation of a BASIC routine I have used for years:

while(substr($temp,0,1) eq " ")
{
$temp = substr($temp,1);
}
while(substr($temp,-1) eq " ")
{
$temp = substr($temp,0,length($temp) - 1);
}

There has to be a simpler way in perl than this.

A regular expression will do:

$temp =~ s/^\s*(.*?)\s*$/$1/;

Of course, this gets rid of any type leading or trailing of whitespace,
which may or may not be what you want. You can switch \s with a space if it
does.

Matt
 
U

usenet

Bill said:
How to in perl

Please see the posting guidelines which Sinan referred you to in an
earlier reply
there has to be an easier way of trimming the spaces off the begining
and ending of a variable...

There are many ways to do it in Perl. Here's my usual:

s/^\s*//, s/\s+$// for $foo;
 
M

Matt Garrish

Gunnar Hjalmarsson said:
Hrrmm..

perldoc -q "strip blank"

Slow and destructive has never bothered me (though if you're running it
(tens of) thousands of times I suppose you might), and the embedded
newlines, if likely, can be easily handled with /s. Whatever you like...

Matt
 
J

John Bokma

Does it make a difference in this situation?

I think yes, but you have to benchmark it. I would use \s+ for both since
I want to drop one or more spaces (and not zero or more)

Strikes me as odd to use \s* for one and \s+ for the other.
 
J

John W. Krahn

Does it make a difference in this situation?

It may not make much of a difference but it does make a difference. \s* will
always match so the replacement will always take place while \s+ will only
match if there is whitespace present so the replacement will not take place if
there is no whitespace.


John
 
J

Jürgen Exner

Bill said:
I do a lot of small routines in perl for manipulating data and know
there has to be an easier way of trimming the spaces off the begining
and ending of a variable. I use the following which is basically a
translation of a BASIC routine I have used for years:

while(substr($temp,0,1) eq " ")
{
$temp = substr($temp,1);
}
while(substr($temp,-1) eq " ")
{
$temp = substr($temp,0,length($temp) - 1);
}

There has to be a simpler way in perl than this.

Is there anything wrong with the answer in the FAQ ("perldoc -q space"):
"How do I strip blank space from the beginning/end of a string?"

jue
 
A

Anno Siegel

Dr.Ruud said:
(e-mail address removed):

Beyond that it hurts my eyes? Let's find out.

Yes, let's. Benchmark (appended below) shows "+" faster than "*".
The speedup is most noticeable (a factor of almost 2) when there
are no blanks to trim and goes down to 1.2 for an average of 15
blanks front and rear. Makes sense.

So, in the rather unlikely case that the time for trimming blanks
matters at all, the difference may be significant.

Anno

-----------------------------------------------------------------

#!/usr/bin/perl
use strict; use warnings; $| = 1; # @^~`
use Vi::QuickFix;

use Benchmark qw( cmpthese);

use constant SIZE => 20;
my @none = ( qw( alpha beta gamma delta)) x SIZE;
my @few = map blanks( 3) . $_ . blanks( 3), @none;
my @many = map blanks( 30) . $_ . blanks( 30), @none;

goto bench;

check:
for ( 1 .. 7 ) {
for ( shift( @none), shift( @few), shift( @many) ) {
print "|$_| -> ";
s/^\s+//, s/\s+$//;
print " |$_|\n";
}
print "--------------------\n";
}
exit;

bench:
print "No Blanks:\n";
my @data = @none;
cmpthese -1, {
plus => sub { s/^\s+//, s/\s+$// for map "$_", @data },
aster => sub { s/^\s*//, s/\s*$// for map "$_", @data },
};

print "\nFew Blanks:\n";
@data = @few;
cmpthese -1, {
plus => sub { s/^\s+//, s/\s+$// for map "$_", @data },
aster => sub { s/^\s*//, s/\s*$// for map "$_", @data },
};

print "\nMany Blanks:\n";
@data = @many;
cmpthese -1, {
plus => sub { s/^\s+//, s/\s+$// for map "$_", @data },
aster => sub { s/^\s*//, s/\s*$// for map "$_", @data },
};

########################################################################

sub blanks { ' ' x rand shift }
 
D

Dr.Ruud

Anno Siegel:
Dr.Ruud:

Yes, let's. Benchmark (appended below) shows "+" faster than "*".
The speedup is most noticeable (a factor of almost 2) when there
are no blanks to trim and goes down to 1.2 for an average of 15
blanks front and rear. Makes sense.

Yes, thanks for that. I inserted a substr() approach like:

my ($i1, $i2);
for ( $i1 = 0 ; ' ' eq substr($_, $i1, 1) ; $i1++ ) {};
for ( $i2 = length() -1 ; ($i2 > $i1) && (' ' eq substr($_, $i2, 1)) ;
$i2-- ) {};
$_ = substr($_, $i1, $i2 - $i1 +1);

and that performed dramatically worse.

So, in the rather unlikely case that the time for trimming blanks
matters at all, the difference may be significant.


There's more to it than time: with s/^\s*// the data will always be
touched (even when the returned list of s/// is not used, unless perl
includes an optimization for that situation).
 
A

A. Sinan Unur

He did have it in the subject of his article...

What he had in the subject of this article was "How to in perl". That
was not what the OP's article was about. The OP's article was about
trimming leading and trailing spaces from a string. Hence, an
appropriate subject line would have been:

Subject: How to trim leading/trailing spaces

But, of course, if the OP had actually read the FAQ, then he would have
known the answer to that question.

And, if you had read the posting guidelines, you would have known what
Tad meant, instead of this silly post.

Sinan
 
B

Bill H

Sorry to have stepped on toes, thank you for at least giving me an
answer.

Didn't know:

1) there was a rule on how to post messages
2) that there was even a perl usenet till I stumbled upon it
3) asking a basic question would get everyone in such an uproar

Maybe I should just go back to searching the internet for information
on how to do something in perl?

All the other usenet groups I post on allow asking questions and
posting opinions. The reception I got here made me feel as if I was in
a upscale restaurant and used the wrong fork to eat my salad.

Bill H
 
B

Bill H

Started reading your guidelines at:

http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

And saw this almost immedietly:

A note to newsgroup ``regulars'':

Do not use these guidelines as a "license to flame" or other
meanness. It is possible that a poster is unaware of things
discussed here. Give them the benefit of the doubt, and just
help them learn how to post, rather than assume

Maybe you guys should read them also
 
P

Paul Lalli

Bill said:
Started reading your guidelines at:

http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

And saw this almost immedietly:

A note to newsgroup ``regulars'':

Do not use these guidelines as a "license to flame" or other
meanness. It is possible that a poster is unaware of things
discussed here. Give them the benefit of the doubt, and just
help them learn how to post, rather than assume

Maybe you guys should read them also

Please point out, specifically, which response you believe was a flame,
or otherwise "meanness". Reviewing the thread, I see nothing of the
sort. I see bluntness, yes, but not cruelty, rudeness, or flaming.

Paul Lalli
 

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

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top