Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String matching at the beginning

Status
Not open for further replies.

rob51383

Programmer
Jun 23, 2004
134
US
I have a variable with value formats like:

$var = "10 some text, numbers";
$var = "1 some text, numbers";
$var = "99232 some numbers, text";
$var = "182 some text, numbers";
(Basically a number a space and anything)

I want to extract the first number from the variable and assign it to another variable So If I extracted "10 some text, numbers" I would be left with:
$newvar = "10";

What is the matching characters that can do this? I greatly appreciate any help.
 
I managed to complete it. I just used the "SPLIT" function to split $var at ' ' into a string then assigned @ARRAY[0] to $newvar.

If you know a better way to do it I am open to change.
 
That approach is probably the best one if you're certain that each string starts with a number followed by a space. You could alternatively use a regexp if the format isn't always going to be that straightforward.
 
Seems wasteful to create an array and then ignore everything but the first element. Why is this better than the regexp approach?
 
If you reuse that array you won't really be wasting much memory, but it still is more lines of code and doesn't look as tidy.

I would recomend a simple regexp such as...
/\d+/o

The \d+ matches 1 or more digits in a row and the o forces only 1 match.

You'll get the same result, and as ishnid was saying, this will also work even when the original string is in a different format.
 
I was thinking more like
Code:
$newvar = $1 if $var =~ /^(\d+)/;
or even
Code:
($newvar) = $var =~ /^(\d+)/;
I'm hazy at best on the /o modifier; I've never used it and seldom seen it used. It wasn't my impression from what I've read in perlretut that it "forces only 1 match". Can you enlighten me, ktulu2?
 
If I remember from back in my days of "Perl in a Nutshell" that the o forces 1 match, but I could be wrong. I never use it because PERL will match only 1 by default. I was just using that to emphasize the regexp only matching the first block of digits.

I'll have to go back and and browse the regexp section of my learning perl books.
 
OK, I looked through various learning PERL books and here is what I found

All that "PERL in a Nutshell" says about 'o' is
Nutshell said:
The /o modifier is for efficiency,

And in "Teach yourself PERL in 21 days" it says
21Days said:
The o option enables you to tell the Perl interpreter that a pattern is to be evaluated only once.

That is probably why I remembered that o forces a single match. But I could be misinterpreting what that book is trying to say.

As for the man pages, all I could find about regexp options was the g option. But I know there are more than that, like 'i' for ignoring case and 'x' to ignore whitespace. So I guess I'm now hazy too about the meaning of o.

Rob, ignore the 'o' for now, you don't really need it. Just use my regexp without the 'o' or one of mikevh's. I appologize for the confusion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top