Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Pattern matching

Reply
Thread Tools

Pattern matching

 
 
Guy
Guest
Posts: n/a
 
      05-17-2009
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


 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      05-17-2009
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/"
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      05-17-2009
"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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
Matching neighbouring words of a pattern using Regex CV Perl 2 08-31-2004 12:27 AM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM
Pattern matching help! grep emails from file! danpres2k Perl 3 08-25-2003 02:47 PM
A newbie question on pattern matching DelphiDude Perl 3 07-26-2003 12:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57