Extracting a numeric value from a string.

S

silverfish

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.
 
D

davidfilmer

silverfish said:
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?"
 
U

Uri Guttman

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
 
U

usenet

Uri said:
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 :^}
 
U

usenet

Lukas said:
$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!"
 
U

usenet

Tad said:
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...
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top