retrieving number from a string

Discussion in 'Perl' started by Corne van Tonder, Apr 11, 2005.

  1. 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
     
    Corne van Tonder, Apr 11, 2005
    #1
    1. Advertising

  2. Corne van Tonder

    surfer dude Guest

    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
     
    surfer dude, Apr 12, 2005
    #2
    1. Advertising

  3. 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
     
    Jürgen Exner, Apr 12, 2005
    #3
  4. Corne van Tonder

    Joe Smith Guest

    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
     
    Joe Smith, Apr 12, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    525
    Pete Becker
    May 21, 2005
  2. ilias
    Replies:
    5
    Views:
    388
    Pete Becker
    Jul 27, 2006
  3. davidj411
    Replies:
    11
    Views:
    1,305
    Kam-Hung Soh
    May 29, 2008
  4. Newbie

    Retrieving Cell Number

    Newbie, Aug 1, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    145
    Eliyahu Goldin
    Aug 2, 2005
  5. Mick White

    Retrieving the column number clicked?

    Mick White, Jul 20, 2004, in forum: Javascript
    Replies:
    2
    Views:
    128
    Mick White
    Jul 21, 2004
Loading...

Share This Page