Firstly, I have a hash:
$hWriteHash{'Identifier'}{'number'} that contains = 1#2#12#A24#48
Then I split this by "#" and then put it in the @arr_of_tenors variable.
Here's the code:
use strict;
use warnings;
use Scalar::Util qw(looks_like_number);
use Data::Dumper;
$nums = $hWriteHash{'Identifier'}{'number'};
my @arr_of_tenors = split("#", $nums);
print("@arr_of_tenors\n");
The output is 1 2 12 A24 48
Now, my goal is if the array has an element that's not an integer which is A24, it will go to die function.
Here's what I've tried.
if(not looks_like_number(@arr_of_tenors)){
die "ERROR: Array has an element that's not an integer.";
}else{
print("All good");
}
Obviously, the only acceptable format should be integers only.
I've tried to use looks_like_number but it didn't work. It always goes to else statement.
I know that there is another option which is grep + regex. But as much as possible, I don't want to use regular expressions on this one if there is a function that does the same job. Also, as much as possible, I don't want to iterate each element.
How does looks_like_number works?
Am I missing something?
Here's yet another way:
use Types::Common qw( Int );
if ( Int->all( @arr_of_tenors ) ) {
# all integers
}
else {
# at least one non-integer
}
And another, because why not?
use Types::Common qw( ArrayRef Int );
if ( ArrayRef->of( Int )->check( \@arr_of_tenors ) ) {
# all integers
}
else {
# at least one non-integer
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With