shorten expression

J

Jeff 'japhy' Pinyan

is there a way to shorten this expression?

$host=~/^(\S*)\s*/; $host=$1;

Well, as it stands, even if the regex FAILS, $host will be set to whatever
$1 was before the regex. Assuming you DON'T want this behavior, then you
can do:

($host) = $host =~ /(\S*)/;

By the way, your regex is weird. The \s* at the end is useless, since it
means it can match zero characters at the end of the regex. Since you
don't even use them, you can remove them. And the ^ anchor isn't needed
either, since (\S*) can match zero characters if necessary.

Another way to do it is

$host =~ s/\s.*//s;

That removes everything from the first space to the end of the string.
But I have a feeling my first approach is better.
 
J

Jeff 'japhy' Pinyan

$host=~s/\s.*//;

That won't remove a trailing newline, which WILL be a problem. Even if
there's nothing after the newline, the original code will give you a
string with NO WHITESPACE at all; yours will keep a newline at the end of
$host.

Either add the /s modifier to the substitution, or take a different
approach like

($host) = $host =~ /(\S*)/;
 
J

Jeff 'japhy' Pinyan

$host =~ s/\s+$//;

How do you know he has a string like "hostname "? Maybe the string is
"hostname ip.ad.dr.ess etc". Just removing the trailing whitespace is not
good enough. The OP clearly needs the content before the first whitespace
(even if it's nothing, it seems).
 
J

John Strauss

That won't remove a trailing newline, which WILL be a problem. Even if
there's nothing after the newline, the original code will give you a
string with NO WHITESPACE at all; yours will keep a newline at the end of
$host.

Either add the /s modifier to the substitution, or take a different
approach like

($host) = $host =~ /(\S*)/;

right.
$host=~s/\s.*//s;


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drop the .thetenant to get me via mail
 
J

John D

ZZT said:
Hi,

is there a way to shorten this expression?

$host=~/^(\S*)\s*/; $host=$1;

thanks!
several ways but how about
$host=~s/\s+.+//;
or
($host)=$host=~/^(\S*)\s*/;

Enjoy
Jd
 
S

Sam Holden

several ways but how about
$host=~s/\s+.+//;

That's not the same, for example $host="abc ".

$host=~s/\s+.*//s;

would be an equivalent expression. or:

$host=~s/\s.*//s;

or
($host)=$host=~/^(\S*)\s*/;

Or even:

($host) = split / /, $host, 2;

Of course there's no need for the \s* in that regex. Since just
/^(\S*)/ matches (and captures) exactly the same things.
 
J

Jeff 'japhy' Pinyan

Jeff 'japhy' Pinyan ([email protected]) wrote on MMMDCV September MCMXCIII
in <URL:)) On Tue, 15 Jul 2003, ZZT wrote:
))
)) >is there a way to shorten this expression?
)) >
)) >$host=~/^(\S*)\s*/; $host=$1;
))
)) Well, as it stands, even if the regex FAILS, $host will be set to whatever
)) $1 was before the regex.

But the only string on which the regexp can fail is a string that
doesn't have a beginning. Now, in perl6, with lazy evaluation, we
might be able to construct strings without an end.

But how do I make a string without a beginning?

I realized that, later. A silly thing to have said.
 
I

Iain Chalmers

Abigail said:
Jeff 'japhy' Pinyan ([email protected]) wrote on MMMDCV September MCMXCIII
in
<URL:)) On Tue, 15 Jul 2003, ZZT wrote:
))
)) >is there a way to shorten this expression?
)) >
)) >$host=~/^(\S*)\s*/; $host=$1;
))
)) Well, as it stands, even if the regex FAILS, $host will be set to
whatever
)) $1 was before the regex.

But the only string on which the regexp can fail is a string that
doesn't have a beginning. Now, in perl6, with lazy evaluation, we
might be able to construct strings without an end.

But how do I make a string without a beginning?

Easy,

#!/usr/bin/perl -w
use strict;
require 6.001;

use Abigail::Strings qw(make_string_with_no_end);

my $noEnd=make_string_with_no_end();
my $noBeginning=reverse($noEnd);

if ($noBeginning =~/^(\S*)\s*/){print "string '$noBeginning' has no beginning!"}

:)

cheers,

big
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top