shorten expression

Discussion in 'Perl Misc' started by ZZT, Jul 15, 2003.

  1. ZZT

    ZZT Guest

    Hi,

    is there a way to shorten this expression?

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

    thanks!
     
    ZZT, Jul 15, 2003
    #1
    1. Advertising

  2. 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. 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.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Jul 15, 2003
    #2
    1. Advertising

  3. On Tue, 15 Jul 2003, John Strauss wrote:

    >On Tue, 15 Jul 2003 16:42:39 +0200
    >ZZT <> wrote:
    >>
    >> is there a way to shorten this expression?
    >>
    >> $host=~/^(\S*)\s*/; $host=$1;

    >
    >$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*)/;

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Jul 15, 2003
    #3
  4. On Tue, 15 Jul 2003, Todd de Gruyl wrote:

    >ZZT wrote:
    >> is there a way to shorten this expression?
    >>
    >> $host=~/^(\S*)\s*/; $host=$1;

    >
    >$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).

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Jul 15, 2003
    #4
  5. ZZT

    John Strauss Guest

    On Tue, 15 Jul 2003 11:36:34 -0400
    Jeff 'japhy' Pinyan <> wrote:
    >
    > On Tue, 15 Jul 2003, John Strauss wrote:
    >
    > >On Tue, 15 Jul 2003 16:42:39 +0200
    > >ZZT <> wrote:
    > >>
    > >> is there a way to shorten this expression?
    > >>
    > >> $host=~/^(\S*)\s*/; $host=$1;

    > >
    > >$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*)/;
    >


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


    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    drop the .thetenant to get me via mail
     
    John Strauss, Jul 15, 2003
    #5
  6. ZZT

    John D Guest

    ZZT <> wrote in message news:<bf13t2$n74$-ag.de>...
    > 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
     
    John D, Jul 15, 2003
    #6
  7. ZZT

    Sam Holden Guest

    On 15 Jul 2003 10:51:04 -0700, John D <> wrote:
    > ZZT <> wrote in message news:<bf13t2$n74$-ag.de>...
    >> Hi,
    >>
    >> is there a way to shorten this expression?
    >>
    >> $host=~/^(\S*)\s*/; $host=$1;
    >>
    >> thanks!

    > 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.

    --
    Sam Holden
     
    Sam Holden, Jul 15, 2003
    #7
  8. On 16 Jul 2003, Abigail wrote:

    >Jeff 'japhy' Pinyan () wrote on MMMDCV September MCMXCIII
    >in <URL:news:p>:
    >)) 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.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Jul 16, 2003
    #8
  9. In article <>,
    Abigail <> wrote:

    > Jeff 'japhy' Pinyan () wrote on MMMDCV September MCMXCIII
    > in
    > <URL:news:p>:
    > )) 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

    --
    "Make some noise. You don't have to join anything; noise need
    not be more than individuals standing up for themselves.
    You don't win every time. But you do win sometimes."
    Bruce Schneier in http://www.counterpane.com/crypto-gram.html
     
    Iain Chalmers, Jul 21, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kenneth Keeley

    Shorten a SQL VarChar result.

    Kenneth Keeley, Oct 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    724
    Scott Mitchell [MVP]
    Oct 28, 2004
  2. =?Utf-8?B?V2ViIFJlc3BvbnNlIFRpbWU=?=

    How can I shorten this time?

    =?Utf-8?B?V2ViIFJlc3BvbnNlIFRpbWU=?=, Oct 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    358
    Scott Allen
    Oct 28, 2004
  3. Crispy
    Replies:
    3
    Views:
    694
    sinclar sodersas
    Jul 12, 2003
  4. Benjamin Rutt

    finding redundant #includes to shorten compile time

    Benjamin Rutt, Feb 5, 2004, in forum: C Programming
    Replies:
    15
    Views:
    1,053
    CBFalconer
    Feb 6, 2004
  5. ben
    Replies:
    8
    Views:
    405
Loading...

Share This Page