Sign in to follow this  
Followers 0
zerodata

Need soem help with perl

3 posts in this topic

Ok I have decided to start learning perl so I have been writing some programs to see what I can or cannot do but I am stuck

I want to check the output of a mathematical function to check if it is an integer or a float So I can use it in an if funtion for instance

if ($var == integer) {

print "$a is an integer.\n";

} elsif ($var == float) {

print "$a is a float.\n";

} else {

print "$a is neither an integer or float.\n";

}

What I do not know how to do is check if the output is an integer a float or a string and I could not find anything in the documentation Any help greatly appreciated as I am new to perl this is probably really simple but I can't figure it out

:help:

0

Share this post


Link to post
Share on other sites

Personally i've never tried to any complex mathematic functions, but from what I know I dont think you can do "if $var == integer/string" etc.

Possibly you could try something like if (($var <= 1) && ($number > 0))

These websites may help

http://www.cotse.com/perl/sprintf.html

http://aspn.activestate.com//ASPN/Referenc...eger_arithmetic

http://www.rocketaware.com/perl/math/int/

http://www.pageresource.com/cgirec/ptut7.htm

0

Share this post


Link to post
Share on other sites

Ok I have solved part of the problem enough for me to write the program I wanted anway

so here is the solution first

if (int($var)==$var) {

print "$var is an integer.\n";

Ok and this the program I wanted it for

# A quick program in perl to find a numbers divisors

# Author zerodata

$a="1"; #Starting divisor

$b="133745531"; # Number to find divisors of

$c="1"; # Initialiased output variable

do

{

$c=$b/$a;

if (int($c)==$c) # Check if number is an integer

{

print "$b divided by $a equals $c\n"; # print result

$a=$a+1; #continue to check for more divisors

}

else {

$a=$a+1; #if not found continue checking

}

}

while($c>0); #stop when result is not greater than 0

This program can also be used to check if a number is a prime or not..

Be warned on very large numbers this program will take quite a while to run the full sequence the example is very quick but try it with a number like 57683921072 and go make a three course meal while waiting for the full output LMAO.

Not very useful but it solved a problem for me and I had some fun finding out how to do it.

:D

There is a more complicated way that you can find out all data types but I have not figured it all out yet when I do I will post it on this topic in case it is useful for someone

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0