Extracting a numeric value from a string.

Discussion in 'Perl Misc' started by silverfish, Feb 8, 2006.

  1. silverfish

    silverfish Guest

    Hi,

    Perl newbie here. I need to extact a numeric value from a string that
    looks like this,

    <b>12,543</b>

    what I did so far,

    # .. some codes to extract the above string, <b>12,543</b> to $1

    $number = $1;
    $number =~ tr/,//; # to remove comma
    $number =~ m/(\d+)/; # to get only numeric value
    print("$number\t");

    unfortunately, the output was <b>12543</b> ..

    any help will be appreciated! thanks.
    silverfish, Feb 8, 2006
    #1
    1. Advertising

  2. silverfish

    Guest

    silverfish wrote:
    > I need to extact a numeric value from a string that looks like this,
    > <b>12,543</b>
    >
    > what I did so far,


    > $number =~ tr/,//; # to remove comma


    You don't want to translate there; you want to substitute:

    $number =~ s/,//g;

    > $number =~ m/(\d+)/; # to get only numeric value


    That doesn't do what you think it does. That binds $number to the
    match, but doesn't modify the value of the bind variable ($number) - it
    acutally populates $1 with the result of the match. You could instead
    do this:

    ($number) = $number =~ m/(\d+)/;

    But you may wish to reexamine your entire approach, BTW. Notably,
    since you seem to be parsing HTML, you may wish to avial yourself of
    one of the excellent HTML parsers already available for you on CPAN.
    At least you should read up on:

    perldoc -q nesting
    "How do I find matching/nesting anything?"

    --
    http://DavidFilmer.com
    , Feb 8, 2006
    #2
    1. Advertising

  3. silverfish

    Uri Guttman Guest

    >>>>> "d" == davidfilmer <> writes:

    d> silverfish wrote:
    >> I need to extact a numeric value from a string that looks like this,
    >> <b>12,543</b>
    >>
    >> what I did so far,


    >> $number =~ tr/,//; # to remove comma


    d> You don't want to translate there; you want to substitute:

    actually he could use tr/// if he added the d (delete) modifier. what he
    did was a no-op (actually it counted commas and tossed that count away).

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Feb 8, 2006
    #3
  4. silverfish

    Guest

    Uri Guttman wrote:
    > actually he could use tr/// if he added the d (delete) modifier.


    Argh - that's TWO extra keystrokes! (compared to an s/// operation).
    All that extra entropy added to the universe! Not to mention the
    severe health risks - go ahead and get yourself carpel tunnel syndrome;
    see if I care :^}

    --
    http://DavidFilmer.com
    , Feb 9, 2006
    #4
  5. silverfish

    Lukas Mai Guest

    schrob:
    > Uri Guttman wrote:
    >> actually he could use tr/// if he added the d (delete) modifier.

    >
    > Argh - that's TWO extra keystrokes! (compared to an s/// operation).


    $str =~ s/,//g;
    $str =~ y/,//d;

    OPTOMIZED!
    Lukas Mai, Feb 9, 2006
    #5
  6. silverfish

    Guest

    Lukas Mai wrote:
    > $str =~ s/,//g;
    > $str =~ y/,//d;
    >
    > OPTOMIZED!


    No, still more entropy added to the universe. Both 's' and 'd' are
    home-row keys on QWERTY keyboards, but you must reach further
    (expending more heat energy) to type a "y" than a "g".

    When the universe goes critical and falls down around us, and the
    earth's atmosphere is sucked into space as the ground turns to dust,
    with my last breath I will say, "It's all Lukas' fault!"

    --
    http://DavidFilmer.com
    , Feb 9, 2006
    #6
  7. <> wrote:
    > Uri Guttman wrote:
    >> actually he could use tr/// if he added the d (delete) modifier.

    >
    > Argh - that's TWO extra keystrokes! (compared to an s/// operation).



    And several hundred? CPU cycles less than the s///, no
    building/executing/tearing down of a finite state machine.

    How Regexes Work:

    http://www.plover.com/~mjd/perl/Regex/


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 9, 2006
    #7
  8. silverfish

    Guest

    Tad McClellan wrote:
    > And several hundred? CPU cycles less than the s///, no
    > building/executing/tearing down of a finite state machine.
    >
    > How Regexes Work:
    >
    > http://www.plover.com/~mjd/perl/Regex/


    Dude, you are giving this WAAAAYYYY more thought than is really merited
    (really, though, I'm sure you realize it's all a joke). But the link
    you provided is a VERY interesting read...

    --
    http://DavidFilmer.com
    , Feb 10, 2006
    #8
    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. david
    Replies:
    7
    Views:
    11,676
    Ricardo Dominguez
    Oct 10, 2012
  2. Replies:
    5
    Views:
    933
    X-Centric
    Jun 30, 2005
  3. darrel
    Replies:
    4
    Views:
    818
    darrel
    Jul 19, 2007
  4. jobs

    int to numeric numeric(18,2) ?

    jobs, Jul 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    963
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 22, 2007
  5. Replies:
    9
    Views:
    593
    James Kanze
    Sep 19, 2007
Loading...

Share This Page