Extracting a numeric value from a string.

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

  1. silverfish

    silverfish Guest


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


    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

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

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

  2. silverfish

    davidfilmer Guest

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

    $number =~ s/,//g;
    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?"
    davidfilmer, Feb 8, 2006
    1. Advertisements

  3. silverfish

    Uri Guttman Guest

    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 Guttman, Feb 8, 2006
  4. silverfish

    usenet Guest

    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 :^}
    usenet, Feb 9, 2006
  5. silverfish

    Lukas Mai Guest

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

    Lukas Mai, Feb 9, 2006
  6. silverfish

    usenet Guest

    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!"
    usenet, Feb 9, 2006

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

    How Regexes Work:

    Tad McClellan, Feb 9, 2006
  8. silverfish

    usenet Guest

    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...
    usenet, Feb 10, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.