removing white space

2002-11-21 Thread Mariusz K



Hi,

One part of my script ads several strings into one:
$text = $part1.$part2.$part3.(...etc)

However, if the part3 through part10 were empty I get that many white spaces 
at the end of $text. I thought the best thing would be just to remove the 
spaces at the end, but how? (maybe search and remove pattern?)

thanks
M


_
Tired of spam? Get advanced junk mail protection with MSN 8. 
http://join.msn.com/?page=features/junkmail


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



removing white space

2001-06-26 Thread David Gilden

Is the following regrex the correct way to remove leading or trailing white space
from a string?

$data = "[EMAIL PROTECTED] (mike smith)"

$data =~ s/(^\s+)|(\s+$)//g;

or would it be more efficient to it thus:

# two passes
$data =~ s/^\s+)//;
$data =~ s/\s+$)//;

Final comment when I am reading from a loop:


open(EL,"$emailLog") ||  &errorLog( "Read in, Could not find $emailLog, $!");
my @lines = ;

 foreach(@lines){
next if /test/; 
$_ =~ s/^\s+(.*)\s+$/\1,/; 

 other code

Do I need to escape the '@' as my data will be of format:
[EMAIL PROTECTED] (name) 


Thanks
Dave



Re: removing white space

2002-11-21 Thread Sudarshan Raghavan
On Fri, 22 Nov 2002, Mariusz K wrote:

> Hi,
> 
> One part of my script ads several strings into one:
> $text = $part1.$part2.$part3.(...etc)
> 
> However, if the part3 through part10 were empty I get that many white spaces 
> at the end of $text. I thought the best thing would be just to remove the 
> spaces at the end, but how? (maybe search and remove pattern?)

perldoc -q space


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: removing white space

2002-11-22 Thread John W. Krahn
Mariusz K wrote:
> 
> Hi,

Hello,

> One part of my script ads several strings into one:
> $text = $part1.$part2.$part3.(...etc)
> 
> However, if the part3 through part10 were empty I get that many white spaces
> at the end of $text. I thought the best thing would be just to remove the
> spaces at the end, but how? (maybe search and remove pattern?)

Why not use an array instead of ten separate scalars?  If the variable
is really empty it wouldn't print out as a space.

$ perl -e'
( $part1, $part2, $part3, $part4, $part5 ) = ( "one", "two", "", undef,
"five" );
$text = $part1.$part2.$part3.$part4.$part5;
print "$text\n";
'
onetwofive


How to remove whitespace at the end (or beginning) of a string is a
Frequently Asked Question.

perldoc -q "How do I strip blank space from the beginning/end of a
string"




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: removing white space

2002-11-22 Thread Mariusz
Thank you for your response. I went to perldoc on the net and couldn't find
any examples or more info on "-q space"

Mariusz


- Original Message -
From: "Sudarshan Raghavan" <[EMAIL PROTECTED]>
To: "Perl beginners" <[EMAIL PROTECTED]>
Sent: Friday, November 22, 2002 1:29 AM
Subject: Re: removing white space


> On Fri, 22 Nov 2002, Mariusz K wrote:
>
> > Hi,
> >
> > One part of my script ads several strings into one:
> > $text = $part1.$part2.$part3.(...etc)
> >
> > However, if the part3 through part10 were empty I get that many white
spaces
> > at the end of $text. I thought the best thing would be just to remove
the
> > spaces at the end, but how? (maybe search and remove pattern?)
>
> perldoc -q space
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: removing white space

2002-11-22 Thread Mariusz
Thanks for replying.
My text data comes from text fields so I read them through param and then in
one instance I combine them into one string. Therefore, I don't need to
remove all white spaces - only the ones at the end.

I tried to lookup the "perldoc -q" on the net but couldn't find anything?
(www.perldoc.com right?)

Mariusz


- Original Message -
From: "John W. Krahn" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, November 22, 2002 2:50 AM
Subject: Re: removing white space


> Mariusz K wrote:
> >
> > Hi,
>
> Hello,
>
> > One part of my script ads several strings into one:
> > $text = $part1.$part2.$part3.(...etc)
> >
> > However, if the part3 through part10 were empty I get that many white
spaces
> > at the end of $text. I thought the best thing would be just to remove
the
> > spaces at the end, but how? (maybe search and remove pattern?)
>
> Why not use an array instead of ten separate scalars?  If the variable
> is really empty it wouldn't print out as a space.
>
> $ perl -e'
> ( $part1, $part2, $part3, $part4, $part5 ) = ( "one", "two", "", undef,
> "five" );
> $text = $part1.$part2.$part3.$part4.$part5;
> print "$text\n";
> '
> onetwofive
>
>
> How to remove whitespace at the end (or beginning) of a string is a
> Frequently Asked Question.
>
> perldoc -q "How do I strip blank space from the beginning/end of a
> string"
>
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: removing white space

2002-11-22 Thread david
Mariusz wrote:

> Thank you for your response. I went to perldoc on the net and couldn't
> find any examples or more info on "-q space"
> 
> Mariusz
> 
> 

on your machine, try the following in the command line:

perldoc -q space

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: removing white space

2002-11-22 Thread John W. Krahn
Mariusz wrote:
> 
> From: "John W. Krahn" <[EMAIL PROTECTED]>
> >
> > How to remove whitespace at the end (or beginning) of a string is a
> > Frequently Asked Question.
> >
> > perldoc -q "How do I strip blank space from the beginning/end of a
> > string"
> 
> Thanks for replying.
> My text data comes from text fields so I read them through param and then in
> one instance I combine them into one string. Therefore, I don't need to
> remove all white spaces - only the ones at the end.
> 
> I tried to lookup the "perldoc -q" on the net but couldn't find anything?
> (www.perldoc.com right?)

"perldoc" is a program that is installed on your computer when you
install Perl.



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: removing white space

2002-11-22 Thread Mark Anderson
>I tried to lookup the "perldoc -q" on the net but couldn't find anything?
>(www.perldoc.com right?)

perldoc is software that generally is installed with perl.

perldoc.com has the same information, but doesn't have a way to use the
flags that the software version has.

The -q flag to perldoc searches the FAQs, so from www.perldoc.com, select
the FAQ index.  Using your browser to search for the string "space" on that
page leads to "How do I strip blank space from the beginning/end of a
string?" within perlfaq4.  If you click on the perlfaq4 link, and again
search on "space", you get a link to that question, clicking on which gives
you

which I won't copy to hear because everyone should be able to find it now on
their own if they want to read it.

/\/\ark


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: removing white space

2001-06-26 Thread Jeff 'japhy' Pinyan

On Jun 26, David Gilden said:

>Is the following regrex the correct way to remove leading or trailing
>white space from a string?

Use two passes.

>$data =~ s/^\s+)//;
>$data =~ s/\s+$)//;

Err, extra )'s in there.

  $data =~ s/^\s+//;
  $data =~ s/\s+$//;

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk?  http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
**  Manning Publications, Co, is publishing my Perl Regex book  **




Re: removing white space

2001-06-26 Thread Jos I. Boumans

Hey david,

this regexp will do the trick for you:
$_ = '[EMAIL PROTECTED] (mike smith)';
s/^\s+|\s+$//g;

you dont need the parenthesis around it, unless you want to capture the
whitespace (which seems futile)
i'd say compiling a regexp twice would be more of a drain on system
resources then an | actually, but if you'd like you can use the benchmark
module to time it.


> $data = "[EMAIL PROTECTED] (mike smith)"
>
> $data =~ s/(^\s+)|(\s+$)//g;
>
> or would it be more efficient to it thus:
>
> # two passes
> $data =~ s/^\s+)//;
> $data =~ s/\s+$)//;


if you're concerned with system performance, then you do NOT want to put an
entire file into memory (especially using an array). it's concidered bad
practice and there've beeen quite a few posts on this list about it.

so here's what you should do in this case, seeing you're testing each
element one by one anyway:

while () {
next if /test/;
s/^\s+|\s+$//g;
other_stuff;
}

escaping the @ is not needed, perl will Do The Right Thing for you =)


 
> Final comment when I am reading from a loop:
>
>
> open(EL,"$emailLog") ||  &errorLog( "Read in, Could not find $emailLog,
$!");
> my @lines = ;
>
>  foreach(@lines){
> next if /test/;
> $_ =~ s/^\s+(.*)\s+$/\1,/;
>
>  other code
>
> Do I need to escape the '@' as my data will be of format:
> [EMAIL PROTECTED] (name)


hth,

Jos Boumans




RE: removing white space

2001-06-26 Thread Peter Cornelius

> Is the following regrex the correct way to remove leading or 
> trailing white space
> from a string?
> 
> $data = "[EMAIL PROTECTED] (mike smith)"
> 
> $data =~ s/(^\s+)|(\s+$)//g;

This looks like it works to me.
 
> or would it be more efficient to it thus:
> 
> # two passes
> $data =~ s/^\s+)//;
> $data =~ s/\s+$)//;
I ran the following code:

use Benchmark;
use strict;
use warnings;

timethese (100,
{ pipe => q{
my $data = "bla\@bla.net (mike smith)";
$data =~ s/(^\s+)|(\s+$)//g;
} ,
 separate => q{
my $data = "bla\@bla.net (mike smith)";
$data =~ s/^\s+//;
$data =~ s/\s+$//;
}
}
);

And got this output...

Benchmark: timing 100 iterations of pipe, separate...
  pipe: 19 wallclock secs (18.02 usr +  0.00 sys = 18.02 CPU) @
55478.50/s (n=100)
  separate:  3 wallclock secs ( 3.72 usr +  0.00 sys =  3.72 CPU) @
269106.57/s (n=100)

So I think breaking it into 2 lines is much more efficient.

 
> Final comment when I am reading from a loop:
> 
> 
> open(EL,"$emailLog") ||  &errorLog( "Read in, Could not find 
> $emailLog, $!");
> my @lines = ;
> 
>  foreach(@lines){
> next if /test/; 
> $_ =~ s/^\s+(.*)\s+$/\1,/; 
> 
>  other code
> 
> Do I need to escape the '@' as my data will be of format:
> [EMAIL PROTECTED] (name) 

No, maybe someone else can explain what's going on inside that avoids this,
but I could swear I've done this sort of thing before and had no problems
with my variable contents being interpolated as lists.

BTW s/// operate on $_ by default, just like the m// in the 'next if' line.
So you could just do 
s/^\s+(.*)\s+$/\1,/; 
on that line.  
Also $1 is preferred over \1 according to my 5.6.1 version.

Peter C.



Re: removing white space

2001-06-27 Thread Yvonne Murphy

Has anyone used the whitespace module. I downloaded it from CPAN but I
couldn't get it to work for me at all.
YM