Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identifying variable type 3

Status
Not open for further replies.

jollekox

Programmer
Feb 3, 2004
23
0
0
NO
Hi,

I am trying to run some lines of code, if a scalar is an float, and not if it is an integer.

Currently, I am trying to do this:

if ( $pages =~ /./ ) {
# do something here
}

But this does not work, any suggestions for a fix?

Thanks in advance!
Best Regards,
Jollekox
 
Or
Code:
if ($pages =~ /(.*)[\.,](.*)/ )
{
    my $before_decimal = $1;
    my $after_decimal = $2;
}

This will make your code compliant with other languages (like dutch) using a "," as decimal symbol.
 
@data = ('1234',
         '0.1',
         '.345',
         'abc.345',
         '1.a');
foreach (@data) {
  if ($_ eq int($_)) {
    print "$_ is an integer\n";
  } elsif ($_ =~ m|^(\d+)?\.\d+$|) {
    print "$_ is a float\n";
  } else {
    print "$_ is not a number at all!\n";
  }
}


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top