Database Reference
In-Depth Information
# a different collation)
$valid
=
1
;
# assume valid until we find out otherwise
foreach
my
$v
(
split
(
/,/
,
$val
))
{
if
(
!
grep
(
/^$v$/i
,
@
{
$info
->
{
values
}}))
{
$valid
=
0
;
# value contains an invalid element
last
;
}
}
}
return
$valid
;
}
For bulk testing, construct a hash from the legal
SET
members. The procedure is the
same as shown previously for producing a hash from
ENUM
elements.
To validate a given input value against the
SET
member hash, convert it to the same
lettercase as the hash keys, split it at commas to get a list of the individual elements of
the value, and then check each one. If any of the elements are invalid, the entire value
is invalid:
$valid
=
1
;
# assume valid until we find out otherwise
foreach
my
$elt
(
split
(
/,/
,
lc
(
$val
)))
{
if
(
!
exists
(
$members
{
$elt
}))
{
$valid
=
0
;
# value contains an invalid element
last
;
}
}
After the loop terminates,
$valid
is true if the value is legal for the
SET
column, and
false otherwise. Empty strings are always legal
SET
values, but this code performs no
special-case test for an empty string. No such test is necessary because in that case the
split()
operation returns an empty list, the loop never executes, and
$valid
remains
true.
12.9. Using a Lookup Table to Validate Data
Problem
You must check values to make sure they're listed in a lookup table.
Solution
Issue statements to check whether the values are in the table. The best way to do this
depends on the number of input values and the table size.