#!/usr/bin/perl

$num=14.45905495;
@post=$1 if ($num=~/(\d+\.\d\d)\d+/);
print "$post[0]\n";

output
----
14.45

explanation
------
\d+ = at least one digit or more
\. = followed by a period.
\d\d = two digits
\d+ = followed by any number of digits

(\d+\.\d\d)
Sets the portion in parens to be memory location $1.

Hope this helps. I am sure there is better way but this should do it.

Paul
> -----Original Message-----
> From: David O'Dell [mailto:[EMAIL PROTECTED]] 
> Sent: Tuesday, December 31, 2002 1:16 PM
> To: [EMAIL PROTECTED]
> Subject: substitution or matching regex
> 
> 
> I have a variable:
> $NUM = '14.45905495';
> and I want to remove the trailing digits and only leave 2 
> after the period so it ends up
> 
> '14.45'
> 
> I've tried to do this but it appears to return as an array 
> and always prints out "1".
> 
> #!perl -w
> 
> $NUM = '14.45905495';
> @POST = ($NUM =~ /\d\.\d{2}/);
> print "new value is $POST[0]\n";
> 
> after being run
> 
> 
> $ ./test_ex.pl
> new value is 1
> 
> 
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to