retrieving number from a string

C

Corne van Tonder

Hey folks,

Here's something I'm struggling with at the moment that I was hoping you
guys and/or girls could help me with.

If I gave you the following two strings...

1 /this/is/a/file/or/directory/path
10 /this/is/a/file/or/directory/path

Now, in each case I need to take retrieve only the numeric data from the
string. This would be easy, if i was using substr() and I was sure that I
was working with only a fixed length on the numeric data, but as you can
see, it could be either 1, 2 or more chars in length. How can I do it?

Many thanks for any replies.

cvt
 
S

surfer dude

While wandering through cyberspace on Mon, 11 Apr 2005 22:26:01 GMT,
Corne van Tonder said ...
Hey folks,

Here's something I'm struggling with at the moment that I was hoping you
guys and/or girls could help me with.

If I gave you the following two strings...

1 /this/is/a/file/or/directory/path
10 /this/is/a/file/or/directory/path

Now, in each case I need to take retrieve only the numeric data from the
string. This would be easy, if i was using substr() and I was sure that I
was working with only a fixed length on the numeric data, but as you can
see, it could be either 1, 2 or more chars in length. How can I do it?

Many thanks for any replies.

cvt


Several possibilities come to mind :

(1) Use pattern matching : match the longest string that begins in the
1st column and consists entirely of numeric characters

(2) Use the split function :
split the input record into fields based on whitespace and the number
will be the 1st input field
 
J

Jürgen Exner

Corne said:
Hey folks,

Here's something I'm struggling with at the moment that I was hoping
you guys and/or girls could help me with.

If I gave you the following two strings...

1 /this/is/a/file/or/directory/path
10 /this/is/a/file/or/directory/path

Now, in each case I need to take retrieve only the numeric data from
the string. This would be easy, if i was using substr() and I was
sure that I was working with only a fixed length on the numeric data,
but as you can see, it could be either 1, 2 or more chars in length.
How can I do it?

Many thanks for any replies.

The most trivial approach is probably: just use it.

my $s = "10 /this/is/a/file/or/directory/path";
print $s + 5;

Note: this will generate a warning under "use warnings;"; so you may want to
disable warnings for this statement.

jue
 
J

Joe Smith

Corne said:
I need to take retrieve only the numeric data from the
string. ... it could be either 1, 2 or more chars in length.

You mean you've never heard of using \d in a regular expression?
It's one of the basic character classes.




if ($string =~ /(\d+)/) {
$number = $1;
} else {
warn "no number detected";
}

-Joe
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top