[17508] in Perl-Users-Digest
Perl-Users Digest, Issue: 4928 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Nov 20 00:10:42 2000
Date: Sun, 19 Nov 2000 21:10:13 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <974697013-v9-i4928@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Sun, 19 Nov 2000 Volume: 9 Number: 4928
Today's topics:
trim() equivalent in Perl? <ayambema@adelphia.net>
Re: trim() equivalent in Perl? fhinchey@my-deja.com
Re: trim() equivalent in Perl? <wyzelli@yahoo.com>
Re: trim() equivalent in Perl? (Logan Shaw)
Re: trim() equivalent in Perl? (Garry Williams)
Re: trim() equivalent in Perl? <ayambema@adelphia.net>
Re: trim() equivalent in Perl? (Tad McClellan)
Re: trim() equivalent in Perl? <wyzelli@yahoo.com>
Re: trim() equivalent in Perl? (Garry Williams)
Re: trim() equivalent in Perl? (Tad McClellan)
Re: trim() equivalent in Perl? <joe+usenet@sunstarsys.com>
Re: trim() equivalent in Perl? <brondsem@my-deja.com>
Re: trim() equivalent in Perl? <wyzelli@yahoo.com>
Re: trim() equivalent in Perl? <wyzelli@yahoo.com>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 20 Nov 2000 02:22:04 GMT
From: "hokiebear" <ayambema@adelphia.net>
Subject: trim() equivalent in Perl?
Message-Id: <gV%R5.1911$tR2.51721@news1.news.adelphia.net>
Is there a trim() equivalent function in PERL (i.e. a way to remove white
space from a sentence)? Thank you!
amba
------------------------------
Date: Mon, 20 Nov 2000 02:47:50 GMT
From: fhinchey@my-deja.com
Subject: Re: trim() equivalent in Perl?
Message-Id: <8va3cl$cmi$1@nnrp1.deja.com>
chomp - removes spaces before and after
-Frank
In article <gV%R5.1911$tR2.51721@news1.news.adelphia.net>,
"hokiebear" <ayambema@adelphia.net> wrote:
> Is there a trim() equivalent function in PERL (i.e. a way to remove
white
> space from a sentence)? Thank you!
>
> amba
>
>
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Mon, 20 Nov 2000 12:42:03 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: trim() equivalent in Perl?
Message-Id: <5z0S5.23$Is1.2469@vic.nntp.telstra.net>
<fhinchey@my-deja.com> wrote in message
news:8va3cl$cmi$1@nnrp1.deja.com...
> chomp - removes spaces before and after
>
Where did you get that idea?
from perlfunc:
chomp
This safer version of chop removes any trailing string that corresponds
to the current value of $/ ....
Normally the default value of $/ is "\n" so chomp removes trailing \n
from a string.
What the OP is possibly wanting is s/\s+//g but since I am not familiar
with trim() I am not really qualified to answer this question.
Wyzelli
--
($a,$b,$w,$t)=(' bottle',' of beer',' on the wall','Take one down, pass
it around');
for(reverse(1..100)){$s=($_!=1)?'s':'';$c.="$_$a$s$b$w\n$_$a$s$b\n$t\n";
$_--;$s=($_!=1)?'s':'';$c.="$_$a$s$b$w\n\n";}print"$c*hic*";
------------------------------
Date: 19 Nov 2000 21:11:27 -0600
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: trim() equivalent in Perl?
Message-Id: <8va4ov$5nm$1@boomer.cs.utexas.edu>
>In article <gV%R5.1911$tR2.51721@news1.news.adelphia.net>,
> "hokiebear" <ayambema@adelphia.net> wrote:
>> Is there a trim() equivalent function in PERL (i.e. a way to remove white
>> space from a sentence)? Thank you!
In article <8va3cl$cmi$1@nnrp1.deja.com>, <fhinchey@my-deja.com> wrote:
> chomp - removes spaces before and after
Actually, chomp just removes whatever the line separator is if happens
to be at the end of the string.
The original poster probably needs to use a regular expression to do
whatever they're trying to do. I'm not sure what they mean by
"sentence", but if the goal is to remove all whitespace from the
beginning and end of a string, I would use this:
$string =~ s/\s+$//m;
$string =~ s/^\s+//m;
Hope that helps.
- Logan
------------------------------
Date: Mon, 20 Nov 2000 03:19:31 GMT
From: garry@zweb.zvolve.net (Garry Williams)
Subject: Re: trim() equivalent in Perl?
Message-Id: <7L0S5.1098$xb1.67292@eagle.america.net>
[post re-ordered]
[fixed broken line wrapping]
On Mon, 20 Nov 2000 02:47:50 GMT, fhinchey@my-deja.com
<fhinchey@my-deja.com> wrote:
>In article <gV%R5.1911$tR2.51721@news1.news.adelphia.net>,
> "hokiebear" <ayambema@adelphia.net> wrote:
>> Is there a trim() equivalent function in PERL (i.e. a way to remove
>> white space from a sentence)? Thank you!
>
>chomp - removes spaces before and after
You not only posted upside down, but your post is incorrect.
The chomp operator neither removes spaces from the front or the back
of a string. It doesn't remove spaces at all. Of course, that would
be obvious, if you read the perlfunc manual page.
To the original poster:
It's not at all clear what "remove white space from a sentence" means,
but the answer may be in the FAQ.
See the perlfaq4 manual page: "How do I strip blank space from the
beginning/end of a string?"
--
Garry Williams
------------------------------
Date: Mon, 20 Nov 2000 03:19:46 GMT
From: "hokiebear" <ayambema@adelphia.net>
Subject: Re: trim() equivalent in Perl?
Message-Id: <mL0S5.1919$tR2.52127@news1.news.adelphia.net>
Thank you all for your input. Let me explain what I would like to do. Say I
have a sentence " This is a great newsgroup". I would like to count the
number of letters in that sentence. Counting the number of letters in the
newsgroup would require my first removing whitespace between the individual
words.
Thank you for any suggestions!
hokiebear <ayambema@adelphia.net> wrote in message
news:gV%R5.1911$tR2.51721@news1.news.adelphia.net...
> Is there a trim() equivalent function in PERL (i.e. a way to remove white
> space from a sentence)? Thank you!
>
> amba
>
>
------------------------------
Date: Sun, 19 Nov 2000 21:22:16 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: trim() equivalent in Perl?
Message-Id: <slrn91h2mo.al3.tadmc@magna.metronet.com>
On Mon, 20 Nov 2000 02:47:50 GMT, fhinchey@my-deja.com
<fhinchey@my-deja.com> wrote:
>chomp - removes spaces before and after
No it doesn't.
Please don't just make stuff up.
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Mon, 20 Nov 2000 13:00:28 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: trim() equivalent in Perl?
Message-Id: <pQ0S5.24$Is1.2514@vic.nntp.telstra.net>
"hokiebear" <ayambema@adelphia.net> wrote in message
news:mL0S5.1919$tR2.52127@news1.news.adelphia.net...
> Thank you all for your input. Let me explain what I would like to do.
Say I
> have a sentence " This is a great newsgroup". I would like to count
the
> number of letters in that sentence. Counting the number of letters in
the
> newsgroup would require my first removing whitespace between the
individual
> words.
> Thank you for any suggestions!
>
Well that is a different fish altogether.
$string= 'This is a great newsgroup';
$chars = $string =~ tr/a-zA-Z/a-zA-Z/;
print $chars;
$chars now has the number of characters in the string, excluding spaces
and all other things. Fast too.
Wyzelli
--
push@x,$_ for(a..z);push@x,' ';
@z='092018192600131419070417261504171126070002100417'=~/(..)/g;
foreach $y(@z){$_.=$x[$y]}y/jp/JP/;print;
------------------------------
Date: Mon, 20 Nov 2000 03:29:29 GMT
From: garry@zweb.zvolve.net (Garry Williams)
Subject: Re: trim() equivalent in Perl?
Message-Id: <tU0S5.1099$xb1.67292@eagle.america.net>
[post re-ordered]
On Mon, 20 Nov 2000 03:19:46 GMT, hokiebear <ayambema@adelphia.net> wrote:
>hokiebear <ayambema@adelphia.net> wrote in message
>news:gV%R5.1911$tR2.51721@news1.news.adelphia.net...
>> Is there a trim() equivalent function in PERL (i.e. a way to remove white
>> space from a sentence)? Thank you!
>>
>Thank you all for your input. Let me explain what I would like to do. Say I
>have a sentence " This is a great newsgroup". I would like to count the
>number of letters in that sentence. Counting the number of letters in the
>newsgroup would require my first removing whitespace between the individual
>words.
>Thank you for any suggestions!
Well, it seems you want to use tr///:
tr/ //d;
--
Garry Williams
------------------------------
Date: Sun, 19 Nov 2000 21:29:48 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: trim() equivalent in Perl?
Message-Id: <slrn91h34s.amr.tadmc@magna.metronet.com>
[ Please put your comments *following* the quoted text that
you are commenting on.
]
On Mon, 20 Nov 2000 03:19:46 GMT, hokiebear <ayambema@adelphia.net> wrote:
>I would like to count the
>number of letters in that sentence.
my $letters = tr/a-zA-Z//; # assuming "sentence" is in $_
>Counting the number of letters in the
>newsgroup would require my first removing whitespace between the individual
^^^^^^^
>words.
No. Counting characters does not require modifying the string.
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: 19 Nov 2000 22:35:49 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: trim() equivalent in Perl?
Message-Id: <m3wvdzs4bu.fsf@mumonkan.sunstarsys.com>
"hokiebear" <ayambema@adelphia.net> writes:
> Thank you all for your input. Let me explain what I would like to do. Say I
> have a sentence " This is a great newsgroup". I would like to count the
> number of letters in that sentence.
How about punctuation, numbers, dashes, "\n", etc. Do you want to count
those too?
> Counting the number of letters in the
> newsgroup would require my first removing whitespace
> between the individual words.
Not necessarily.
> Thank you for any suggestions!
Have a look at
perldoc perlop
There is a nice way to do this using something like y/\w//,
but ultimately it will depend on precisely what you want to
count. If you can't figure it out yourself, see
perldoc -q count
for a good hint.
HTH.
--
Joe Schaefer
------------------------------
Date: Mon, 20 Nov 2000 03:25:34 GMT
From: Dave Brondsema <brondsem@my-deja.com>
Subject: Re: trim() equivalent in Perl?
Message-Id: <8va5ja$ee2$1@nnrp1.deja.com>
In article <5z0S5.23$Is1.2469@vic.nntp.telstra.net>,
"Wyzelli" <wyzelli@yahoo.com> wrote:
> <fhinchey@my-deja.com> wrote in message
> news:8va3cl$cmi$1@nnrp1.deja.com...
> > chomp - removes spaces before and after
> >
>
> Where did you get that idea?
>
> from perlfunc:
>
> chomp
>
> This safer version of chop removes any trailing string that
corresponds
> to the current value of $/ ....
>
> Normally the default value of $/ is "\n" so chomp removes trailing \n
> from a string.
>
> What the OP is possibly wanting is s/\s+//g but since I am not
familiar
> with trim() I am not really qualified to answer this question.
trim normally removes all leading and trailing spaces from a string.
So I think $string =~ s/([\s]*)(.*)([\s*])/$2/; will work. I probably
have too many [] or () because I'm not good at getting the best regex
possible.
>
> Wyzelli
> --
> ($a,$b,$w,$t)=(' bottle',' of beer',' on the wall','Take one down,
pass
> it around');
> for(reverse(1..100)){$s=($_!
=1)?'s':'';$c.="$_$a$s$b$w\n$_$a$s$b\n$t\n";
> $_--;$s=($_!=1)?'s':'';$c.="$_$a$s$b$w\n\n";}print"$c*hic*";
>
>
--
Dave Brondsema
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Mon, 20 Nov 2000 14:00:57 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: trim() equivalent in Perl?
Message-Id: <1J1S5.31$Is1.2398@vic.nntp.telstra.net>
"Dave Brondsema" <brondsem@my-deja.com> wrote in message
news:8va5ja$ee2$1@nnrp1.deja.com...
>
> trim normally removes all leading and trailing spaces from a string.
>
> So I think $string =~ s/([\s]*)(.*)([\s*])/$2/; will work. I probably
> have too many [] or () because I'm not good at getting the best regex
> possible.
>
$string =~ s/^\s*(.*\S)\s*$/$1/;
Remove all leading and trailing spaces in one operation.
Wyzelli
--
push@x,$_ for(a..z);push@x,' ';
@z='092018192600131419070417261504171126070002100417'=~/(..)/g;
foreach $y(@z){$_.=$x[$y]}y/jp/JP/;print;
------------------------------
Date: Mon, 20 Nov 2000 14:09:48 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: trim() equivalent in Perl?
Message-Id: <kR1S5.33$Is1.2420@vic.nntp.telstra.net>
"Dave Brondsema" <brondsem@my-deja.com> wrote in message
news:8va5ja$ee2$1@nnrp1.deja.com...
>
> trim normally removes all leading and trailing spaces from a string.
>
> So I think $string =~ s/([\s]*)(.*)([\s*])/$2/; will work. I probably
> have too many [] or () because I'm not good at getting the best regex
> possible.
Yes :(
The [] around the \s are redundant as \s IS a character class.
Why capture the stuff you are just discarding? $1 and $3 are wasted
captures.
.* will gobble everything up to the end of the string, and needs to be
anchored at the last 'non space' or the trailing spaces get captured as
part of '.*'.
$string =~ s/\s*(.*\S)\s*/$1/;
That solves all the above, and omits the unnecessary anchors I have in
my other post.
Wyzelli
--
#Modified from the original by Jim Menard
for(reverse(1..100)){$s=($_!=1)? 's':'';print"$_ bottle$s of beer on the
wall,\n";
print"$_ bottle$s of beer,\nTake one down, pass it around,\n";
$_--;$s=($_==1)?'':'s';print"$_ bottle$s of beer on the
wall\n\n";}print'*burp*';
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V9 Issue 4928
**************************************