http://www.perlmonks.org?node_id=4682

vroom has asked for the wisdom of the Perl Monks concerning the following question:

How do I get the value of a number that is part of a string?

Originally posted as a Categorized Question.

  • Comment on How do I get the value of a number that is part of a string?

Replies are listed 'Best First'.
Re: How do I get the value of a number that belongs to a string??
by chromatic (Archbishop) on Feb 19, 2000 at 02:07 UTC
    Perl will automatically do the right thing with a scalar depending on the context in which you use it. If you were to say:
    my $x = "1"; my $y = "2"; print $x + $y, "\n";
    you'll get 3. If you say:
    my $x = "1"; my $y = "2"; print $x . $y, "\n";
    you'll get 12 ("2" appended to "1"). To force interpretation in a numeric context, you can try:
    my $x = "10abc"; print $x + 0, "\n";
    and you'll get 10. Note that this only works on scalars starting with digits. Strip them out with a regex if you need to.
Re: How do I get the value of a number that is part of a string?
by lalitbans (Novice) on Feb 01, 2010 at 10:48 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.