Pattern matching

Discussion in 'Perl Misc' started by Guy, May 17, 2009.

  1. Guy

    Guy Guest

    I found this example, which you may recognize, they start off with data,
    notice the indentation:

    Skipper
    blue_shirt
    hat
    Professor
    sunscreen
    water_bottle
    Gilligan
    red_shirt
    hat

    Then, as they read the lines of data, they want to determine if the data is
    the person's name, or an item belonging to that person. They use the
    following:

    if (/^(\S.*)/) {...

    This will determine whether or not there is a non-white space (person name)
    at the begining of the line. Would the following achieve the same, in this
    case? If so, is there an advantage to their pattern?

    if (/^\S/) {...

    Thanks,
    Guy
    Guy, May 17, 2009
    #1
    1. Advertising

  2. Guy <> wrote:

    > I found this example, which you may recognize,



    Errr, yes I do.


    > they start off with data,
    > notice the indentation:
    >
    > Skipper
    > blue_shirt
    > hat
    > Professor
    > sunscreen
    > water_bottle
    > Gilligan
    > red_shirt
    > hat
    >
    > Then, as they read the lines of data, they want to determine if the data is
    > the person's name, or an item belonging to that person. They use the
    > following:
    >
    > if (/^(\S.*)/) {...
    >
    > This will determine whether or not there is a non-white space (person name)
    > at the begining of the line.



    .... and additionally saves the line (minus newline) into the $1 variable.


    > Would the following achieve the same,



    That depends on whether the $1 variable is subsequently used or not.


    > in this
    > case?



    You have not shown enough of "this case" to be able to answer your question.


    > If so, is there an advantage to their pattern?



    It saves the line (minus newline) into the $1 variable.

    That would be an advantage if the code later makes use of
    the $1 variable's value.


    > if (/^\S/) {...



    That does not put anything into the $1 variable.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, May 17, 2009
    #2
    1. Advertising

  3. "Guy" <> wrote:
    >I found this example, which you may recognize, they start off with data,
    >notice the indentation:
    >
    >Skipper
    > blue_shirt
    > hat
    >Professor
    > sunscreen
    > water_bottle
    >Gilligan
    > red_shirt
    > hat
    >
    >Then, as they read the lines of data, they want to determine if the data is
    >the person's name, or an item belonging to that person. They use the
    >following:
    >
    >if (/^(\S.*)/) {...
    >
    >This will determine whether or not there is a non-white space (person name)
    >at the begining of the line. Would the following achieve the same, in this
    >case?
    >
    >if (/^\S/) {...


    For the part of the code you showed to us: yes

    >If so, is there an advantage to their pattern?


    It also captures the whole name if there is a name

    jue
    Jürgen Exner, May 17, 2009
    #3
    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. DelphiDude
    Replies:
    3
    Views:
    1,160
  2. danpres2k
    Replies:
    3
    Views:
    7,456
    danpres2k
    Aug 25, 2003
  3. CV
    Replies:
    2
    Views:
    582
    Charles DeRykus
    Aug 31, 2004
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    220
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    214
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page