Pattern matching

G

Guy

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
 
T

Tad J McClellan

Guy said:
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.
 
J

Jürgen Exner

Guy said:
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
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top