Abdullah Shaik Abdullah Shaik - 23 days ago 9
Perl Question

Checking whether NRIC is valid

I was asked to make the script to check whether the Singapore NRIC is valid. I also refer to the link,
https://ayumilovemaple.wordpress.com/2008/09/24/validation-singapore-nric-number-verification/

https://gist.github.com/eddiemoore/7131781

on how to perform validation. I have completed the code and it worked. I am not so sure on whether is my code is is correctly configured.The if statement is somewhat a repetition, so is there another solution for a better one. Can you please improve my code. Here is my code below.

#!/usr/bin/perl

use warnings;
$nric = "F1234567B";

if ($nric !~ /^[STFG][0-9]{7}[A-Z]$/)
{
print "NRIC is not valid";
}
else
{
@values = split("",$nric);
chomp($values[8]);
$total = $values[1]*2+$values[2]*7+$values[3]*6+$values[4]*5+$values[5]*4+$values[6]*3+$values[7]*2;
if ($nric =~ /^[TG]/)
{
$total = $total +4;
}
$remainder = $total % 11;
if ($nric =~ /^[ST]/)
{
if ($remainder = 0 && $nric =~/J$/)
{
print "it is valid\n";
}
elsif ($remainder = 1 && $nric =~/Z$/)
{
print "it is valid\n";
}
elsif ($remainder = 2 && $nric =~/I$/)
{
print "it is valid\n";
}
elsif ($remainder = 3 && $nric =~/H$/)
{
print "it is valid\n";
}
elsif ($remainder = 4 && $nric =~/G$/)
{
print "it is valid\n";
}
elsif ($remainder = 5 && $nric =~/F$/)
{
print "it is valid\n";
}
elsif ($remainder = 6 && $nric =~/E$/)
{
print "it is valid\n";
}
elsif ($remainder = 7 && $nric =~/D$/)
{
print "it is valid\n";
}
elsif ($remainder = 8 && $nric =~/C$/)
{
print "it is valid\n";
}
elsif ($remainder = 9 && $nric =~/B$/)
{
print "it is valid\n";
}
elsif ($remainder = 10 && $nric =~/A$/)
{
print "it is valid\n";
}
else
{
if ($remainder = 0 && $nric =~/X$/)
{
print "it is valid\n";
}
elsif ($remainder = 1 && $nric =~/W$/)
{
print "it is valid\n";
}
elsif ($remainder = 2 && $nric =~/U$/)
{
print "it is valid\n";
}
elsif ($remainder = 3 && $nric =~/T$/)
{
print "it is valid\n";
}
elsif ($remainder = 4 && $nric =~/R$/)
{
print "it is valid\n";
}
elsif ($remainder = 5 && $nric =~/Q$/)
{
print "it is valid\n";
}
elsif ($remainder = 6 && $nric =~/P$/)
{
print "it is valid\n";
}
elsif ($remainder = 7 && $nric =~/N$/)
{
print "it is valid\n";
}
elsif ($remainder = 8 && $nric =~/M$/)
{
print "it is valid\n";
}
elsif ($remainder = 9 && $nric =~/L$/)
{
print "it is valid\n";
}
elsif ($remainder = 10 && $nric =~/K$/)
{
print "it is valid\n";
}
else
{
print "it is not valid\n";
}
}

}


When I run the script it states

Found = in conditional, should be == at perl_validnric1.pl line 22.
Found = in conditional, should be == at perl_validnric1.pl line 73.
it is not valid


The output is correct but I am not sure on whether my code is correctly configured. Please help me improve my code as well.My If statement is quite a repetition, so is it possible to make it smaller using array or other ideas which I do not know of.Please help me. thank you.

Answer Source

Conditional matches should always use == for strings or eq for numbers.

Change your conditional if and elsif statements from = to == as = is not valid. = is purely used to define a value for something i.e. $string = "my string";

example:

elsif ($remainder = 2 && $nric =~/I$/)

to

elsif ($remainder == 2 && $nric =~/I$/)

and Lastly, you are doing a lot of if elseif statements, perhaps you should consider a hash table or just do some more regex matches and your script would be much more elegant.