CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Regular expressions needed..

 



dsatish
New User

Apr 20, 2009, 11:12 PM

Post #1 of 8 (4476 views)
Regular expressions needed.. Can't Post

Could someone help me to write regular expressions for below two cases.

CASE 1:- Want check whether a number contains a decimal or not. If not, i want to take only first two digits from that number.

Example:- if number is 8494948474 , first i want to check for decimal point (.) in this number, if it is not there, then i want to take only first two digits (84) from this number.


CASE 2:- If a number contains a decimal point then i want to take only first two digits and next two digits from the decimal.

Example:- if number is 12.89383787, then i want to print 12.89


KevinR
Veteran


Apr 20, 2009, 11:21 PM

Post #2 of 8 (4475 views)
Re: [dsatish] Regular expressions needed.. [In reply to] Can't Post

It would be nice to see some effort on your part first to try and solve your requirements, even if it does not work.


Code
$num = 12.89383787; 
if ($num =~ /(\d\d\.\d\d)/) {
$num = $1;
}
else {
$num = substr $num,0,2;
}
print $num;


Same thing using the ternary operator:


Code
$num = 1289383787; 
$num = $num =~ /(\d\d\.\d\d)/ ? $1 : substr $num,0,2;
print $num;

-------------------------------------------------


(This post was edited by KevinR on Apr 20, 2009, 11:23 PM)


ichi
User

Apr 21, 2009, 1:25 AM

Post #3 of 8 (4471 views)
Re: [dsatish] Regular expressions needed.. [In reply to] Can't Post

you can take a look at perldoc -q "How do I determine whether a scalar is a number/whole/integer/float" to see how to determine decimals.
otherwise,

Code
$num = 4131231; 
if ( $num =~ /^\d+$/){
print substr($num,0,2);
}



dsatish
New User

Apr 21, 2009, 2:07 AM

Post #4 of 8 (4469 views)
Re: [ichi] Regular expressions needed.. [In reply to] Can't Post

Hi all,

Thank you so much for reply. I made a small modifiction by including || operator in the middle, but it is not working. What is my mistake. My intention here is , i want to check for 3 digits or 2 digits before the dot(.) operator and i want to print the number

12.89383787 ---> 12.89 is expected output.

128.9383787 ---> 128.93 is expected output. like this...

$num = 12.89383787;
$num = $num =~ /(\d\d\d\.\d\d)/ || /(\d\d\.\d\d)/ ? $1 : substr $num,0,2;
print $num;

(Or,)

In a float number, i want to take all the digits left side of the dot (.) and 2 digits after the dot (.)


ichi
User

Apr 21, 2009, 2:53 AM

Post #5 of 8 (4465 views)
Re: [dsatish] Regular expressions needed.. [In reply to] Can't Post

then you can just use print format specifiers to specify 2 decimal places. Or am i misinterpreting something ?


dsatish
New User

Apr 21, 2009, 3:10 AM

Post #6 of 8 (4463 views)
Re: [ichi] Regular expressions needed.. [In reply to] Can't Post

Thank you all for your replies. Just did some R&D and got it. here is my piece of code i have written...

$num = 128.9383787;
$num = $num =~ /^[+-]?(\d+\.\d)/ ? $1 : substr $num,0,2;
print $num;


ichi
User

Apr 21, 2009, 3:21 AM

Post #7 of 8 (4461 views)
Re: [dsatish] Regular expressions needed.. [In reply to] Can't Post


In Reply To
Thank you all for your replies. Just did some R&D and got it. here is my piece of code i have written...

$num = 128.9383787;
$num = $num =~ /^[+-]?(\d+\.\d)/ ? $1 : substr $num,0,2;
print $num;

the output has only 1 decimal. not what you want right?


Shree
Novice


Apr 22, 2009, 12:20 AM

Post #8 of 8 (4410 views)
Re: [ichi] Regular expressions needed.. [In reply to] Can't Post

But in your question you have mentioned that if the value of $num = 128.9383787;

Then you want to take only the 4 digits in which 2 digits are before the decimal point '.' and 2 digits after decimal;
and here it should be 28.93

The above code is producing the output: 128.9

Thanks
-Shree


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives