Jump to content


Photo
- - - - -

Need soem help with perl


  • Please log in to reply
2 replies to this topic

#1 zerodata

zerodata

    Gibson Hacker

  • Members
  • 88 posts

Posted 07 December 2002 - 06:25 PM

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:

#2 nick84

nick84

    Member

  • Agents of the Revolution
  • 1,680 posts
  • Gender:Male

Posted 07 December 2002 - 07:25 PM

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.activest...eger_arithmetic
http://www.rocketawa.../perl/math/int/
http://www.pageresou...girec/ptut7.htm

#3 zerodata

zerodata

    Gibson Hacker

  • Members
  • 88 posts

Posted 07 December 2002 - 10:41 PM

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




BinRev is hosted by the great people at Lunarpages!