Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > retrieving number from a string

Reply
Thread Tools

retrieving number from a string

 
 
Corne van Tonder
Guest
Posts: n/a
 
      04-11-2005
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
 
Reply With Quote
 
 
 
 
surfer dude
Guest
Posts: n/a
 
      04-11-2005
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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-12-2005
Corne van Tonder wrote:
> 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


 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      04-12-2005
Corne van Tonder wrote:
> 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
 
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
Retrieving Motherboard Serial Number Oscar Lok Ruby 1 09-21-2010 08:51 PM
retrieving random number generator state and other RNG questions ilias C++ 5 07-27-2006 03:17 AM
OT: Number Nine, Number Nine, Number Nine Frisbee® MCSE 37 09-26-2005 04:06 PM
Retrieving Cell Number Newbie ASP .Net Datagrid Control 3 08-02-2005 04:32 PM
Retrieving the column number clicked? Mick White Javascript 2 07-21-2004 09:54 AM



Advertisments