Re: Error on Datatype Validation
|
Michael Calcupp |
|
12/15/2007 3:06:16 AM |
Tangama, Put the CheckVariables code in the same directory as the script that calls it, name it CheckVariables.pm, and you can use it like so: Code: ( text ) but if you look at the source code you will see it's using three simple regxeps to determine the type of data: Code: ( text ) my %hash = (integer => qr {^[-+]?\d+$} , float => qr {^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$} , string => qr {.+} ,);
you could just use that in your code, but if you are not sure how then use the module as described above. Post Comments |
|
Hi Micheal, I can't digest what u mentioned.... Post Comments |
|
Just do it like this: Code: ( text ) my %hash = (integer => qr {^[-+]?\d+$} , float => qr {^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$} , string => qr {.+} ,); my $foo = 'test'; my $type = check_type($foo) || 'unknown'; print $type; sub check_type { my $t = shift or return(0); foreach my $type qw(integer float string) { if ($t =~ $hash {$type} ) { return $type; }
}
}
Post Comments |
|
Michel. Thanks for your quick reply. Post Comments |
|
Hi Michel, Thanks a lot for this piece of code. This has really helped me. But, I am experiencing errors when I am executing this. It says
syntax error at line 13, near "$type qw(integer float string)"
Could you help me out with this.
Thanks..!!!
Post Comments |
|