regexp + floor

L

lemon

Hi all,

I have the following pattern in my code

<area alt="IT" shape="rect" coords="24,72,109,131"

and would like to apply multiply each of the integers in coords by a
coeficient, and round it.
Can one do this inside a regexp?
I've looked for a round funciton, but found none...

Thanx

Lemon
 
U

usenet

lemon said:
I've looked for a round funciton, but found none...

It's not called 'round'.

perdoc -f sprintf

For example, to round to 5 decimal places:

my $pi = "3.1415927"
my $rounded = sprintf ("%.3f", $pi); # 3.143

See also Number::Format (to make life easier)
 
A

Anno Siegel

.. eh, make that thee decimal places for the example I showed...
Further:

my $pi = "3.1415927"
my $rounded = sprintf ("%.3f", $pi); # 3.143
^^^^^
Make that 3.142. Computer arithmetic is weird, but not that weird :)

Anno
 
E

Eric J. Roode

Hi all,

I have the following pattern in my code

<area alt="IT" shape="rect" coords="24,72,109,131"

and would like to apply multiply each of the integers in coords by a
coeficient, and round it.
Can one do this inside a regexp?
I've looked for a round funciton, but found none...

Try:

$string =~ s/(\d+)/ sprintf '%.0f', $1 * $coefficient /eg;

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
T

Tad McClellan

lemon said:
I have the following pattern in my code

<area alt="IT" shape="rect" coords="24,72,109,131"

and would like to apply multiply each of the integers in coords by a
coeficient, and round it.
Can one do this inside a regexp?


No.

You could, however, do it in the replacement string part
of an s///e operator.

I've looked for a round funciton, but found none...


You must not have looked very hard.

You are expected to check the Perl FAQ *before* posting
to the Perl newsgroup:

perldoc -q round

Does Perl have a round() function? What about ceil() and floor()?
Trig functions?
 

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

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top