matching only white space

R

Robin

I dunno, would this match only white space: / */ . ... ?
or would it match somethin else with the white space?
 
A

A. Sinan Unur

Robin said:
I dunno, would this match only white space: / */ . ... ?
or would it match somethin else with the white space?

What happened when you tried it?

use strict;
use warnings;

my @strings = (' ', ' another', 'another one', 'o n e m o r e');

for (@strings) {
print "$_: matched\n" if / */;
}

If you want to match a line containing nothing but whitespace, then say so:

print "$_: matched\n" if /^\s*$/;

Feel free to read the docs: perldoc perlre

Sinan.
 
R

Robin

What happened when you tried it?

use strict;
use warnings;

my @strings = (' ', ' another', 'another one', 'o n e m o r e');

for (@strings) {
print "$_: matched\n" if / */;
}

Thanks, that's all I needed to know, I'll have to read that perldoc...
peace,
-Robin
 
R

Robin

I suppose I'll just skip doing that site, I don't quite have ADD, although
I'm still working on it, check out the beginning of the site anyway...
http://www.reachcac.org/robin/perl/index.html is it still too official to
say "a homepage"? If anyone is interested in co-designing it or posting
tutorials/scripts, let me know...

-Thanks,
Robin

PS. I don't like being killfiled and flamed, just trying to help a starving
newsgroup :), yeah, right...
 
S

Sherm Pendley

I dunno, would this match only white space: / */ . ... ?
or would it match somethin else with the white space?

No. It matches zero or more occurences of a space character. To match
white space, use \s, which includes tabs, newlines, and other characters
along with literal space characters.

See also:

perldoc perlretut

It occurs to me that you've often been told "perldoc this", but may not
have been told what "perldoc" is...

"perldoc" is a utility that's used to view the documentation that's
bundled with Perl. To use it, open up a terminal window and type "perldoc
whatever". Some good places to start are "perldoc perlfaq", "perldoc
perl", and "perldoc intro". The same documentation can also be found in
HTML format at <http://www.perldoc.com>.

I *think* perldoc is available on WinDOS systems too, but I don't have one
handy to check.

sherm--
 
J

Jürgen Exner

Robin said:
I dunno, would this match only white space: / */ . ... ?
or would it match somethin else with the white space?

Why do you think a plain blank character would match any arbitrary white
space?
If you want to match white space then say so
/\s*/

For further details please Read The Fine Manual: perldoc perlre

jue
 
M

Marc Bissonnette

No. It matches zero or more occurences of a space character. To match
white space, use \s, which includes tabs, newlines, and other
characters along with literal space characters.

See also:

perldoc perlretut

It occurs to me that you've often been told "perldoc this", but may
not have been told what "perldoc" is...

"perldoc" is a utility that's used to view the documentation that's
bundled with Perl. To use it, open up a terminal window and type
"perldoc whatever". Some good places to start are "perldoc perlfaq",
"perldoc perl", and "perldoc intro". The same documentation can also
be found in HTML format at <http://www.perldoc.com>.

I *think* perldoc is available on WinDOS systems too, but I don't have
one handy to check.

Yup, I've got ActiveState perl (build 807) on an WinXP box and the
perldoc functions fine in a dos window.

If (s)he finds it inconvenient to read the perldocs in the dos window,
perldoc whatever >whatever.txt
dumps the output of the 'perldoc whatever' into whatever.txt for printing
/ bathroom reading / etc.
 
A

A. Sinan Unur

....

....

....

If (s)he finds it inconvenient to read the perldocs in the dos window,
perldoc whatever >whatever.txt
dumps the output of the 'perldoc whatever' into whatever.txt for
printing / bathroom reading / etc.

There is also HTML documentation, you know:

C:\Perl\html\perltoc.html

Change C:\Perl to the directory where Perl is installed.
 
M

Matt Garrish

Jürgen Exner said:
Why do you think a plain blank character would match any arbitrary white
space?
If you want to match white space then say so
/\s*/

For further details please Read The Fine Manual: perldoc perlre

Everyone seems to be missing a fundemental point about optional content:

my $line = 'IHAVENOWHITESPACE';

if ($line =~ /\s*/) {
print "D'oh!";
}


Matt
 
M

Matt Garrish

Jürgen Exner said:
Why do you think a plain blank character would match any arbitrary white
space?
If you want to match white space then say so
/\s*/

For further details please Read The Fine Manual: perldoc perlre

Everyone seems to be missing a fundemental point about optional content:

my $line = 'IHAVENOWHITESPACE';

if ($line =~ /\s*/) {
print "D'oh!";
}


Matt
 
M

Matt Garrish

My apologies for the duplicate posting. My news server crapped out while I
was trying to send, but seems to have sent the first copy anyway...

Matt
 
T

Tad McClellan

Marc Bissonnette said:
If (s)he finds it inconvenient to read the perldocs in the dos window,
perldoc whatever >whatever.txt
dumps the output of the 'perldoc whatever' into whatever.txt for printing
/ bathroom reading / etc.


Do the pod2* converters (eg: pod2html) work?
 
T

Tad McClellan

Robin said:
PS. I don't like being killfiled and flamed,


Do you expect folks to remain silent when you take cuts in line?

That is a pretty unreasonable expectation...


[snip] I view a programming language as a place to be
explored, like Disneyland. You don't need to have a lot of preparation
to explore a theme park. You do have to go along with the crowd
control measures, though. In a sense, each ride has its own
prerequisites--if you cut in line, you risk getting tossed out of the
park.

What we have here in this newsgroup is a failure in crowd control.
Reading the FAQ is like staying in line--it's something you should
learn in kindergarten. Usenet needs a better kindergarten.
 
A

Anno Siegel

Matt Garrish said:
Everyone seems to be missing a fundemental point about optional content:

my $line = 'IHAVENOWHITESPACE';

if ($line =~ /\s*/) {
print "D'oh!";
}

So? It still didn't match anything but spaces.

Anno
 
M

Matt Garrish

Anno Siegel said:
So? It still didn't match anything but spaces.

The spaces between spaces, I suppose, but not what the OP wanted to know
(and not \s solutions that have been posted). Writing a regex with nothing
but optional content is just pointless in so many ways it boggles my poor
mind...

Matt
 
A

Anno Siegel

Matt Garrish said:
The spaces between spaces, I suppose, but not what the OP wanted to know
(and not \s solutions that have been posted). Writing a regex with nothing
but optional content is just pointless in so many ways it boggles my poor
mind...

As a stand-alone regex it wouldn't do much, though with /g it may serve
a purpose. As part of a larger regex it is perfectly useful.

I'm not sure what the OPs original question was.

Anno
 
M

Matt Garrish

Anno Siegel said:
As a stand-alone regex it wouldn't do much, though with /g it may serve
a purpose. As part of a larger regex it is perfectly useful.

True enough, though I personally can't think of any reason why you would
want to match/count these "spaces" in a string (but I suppose everything has
its purpose). length() would be the function most people would be looking
for, or so I would assume, as a count based on an optional pattern like the
above will always return the actual length plus one (that pesky "space" at
the beginning of the string).
I'm not sure what the OPs original question was.

My understanding, at any rate, was that he had stumbled upon the above
nuance of regexes. In the lingo of today's youth he wrote:

I dunno, would this match only white space: / */ . ... ?
or would it match somethin else with the white space?

I will be so bold as to suggest that his understanding of whitespace is not
on par with yours... : )

Matt
 
A

Anno Siegel

Matt Garrish said:
True enough, though I personally can't think of any reason why you would
want to match/count these "spaces" in a string (but I suppose everything has
its purpose). length() would be the function most people would be looking
for, or so I would assume, as a count based on an optional pattern like the
above will always return the actual length plus one (that pesky "space" at
the beginning of the string).

The length? You mean it matches once for every character, and then one
more? / */ is greedy, it will still match consecutive blanks in one
go. I wouldn't know how to characterize what it counts in a few words,
but it's not simply related to string length.

Anno
 
M

Marc Bissonnette

Do the pod2* converters (eg: pod2html) work?

I suspect that they do, but when I try it:
############################################
pod2html -infile=c:/perl/lib/pod/perlretut.pod -outfile=perlretut.html -
podpath=c:/perl/lib/pod/ -podroot c:/perl/lib/pod/
############################################

I get "No perl script found in input"

Now that you've made me think of it and try it - I'd be curious if someone
could point me in the right direction as to why the above doesn't work...
 

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
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top