Warning - "Odd number of items in hash assignment" in perl
I get a warning using the following syntax -
my %data_variables = ("Sno." => (5,0), "ID" => (20,1), "DBA" => (50,2), "Address" => (80,3), "Certificate" => (170,4), ); But I do not get a similar warning when using similar syntax.
my %patterns = ("ID" => ("(A[0-9]{6}?)"), "Address" => (">([^<]*<br[^>]+>[^<]*)<br[^>]+>Phone"), "Phone" => ("Phone: ([^<]*)<"), "Certificate" => ("(Certificate [^\r\n]*)"), "DBA" => ("<br[^>]+>DBA: ([^<]*)<br[^>]+>"), ); You need to change parentheses to square brackets:
my %data_variables = ( "Sno." => [5,0], "ID" => [20,1], "DBA" => [50,2], "Address" => [80,3], "Certificate" => [170,4], ); Hash values โโmust be scalar values, so your number lists should be stored as array references (hence, square brackets).
In your second example, the brackets are redundant and just confuse the question. Each set of brackets contains only one scalar value (string), each of which becomes a hash value.
The difference is that "..." is a string (single scalar), and (5, 0) is a list of two scalars. So, in the first snippet, you actually do this:
my %data_variables = ("Sno.", 5, 0, "ID", 20, 1, "Address", 80, 3, "Certificate", 170, 4); Since hashes are just lists with an even number of elements, this will work when the number of elements is even, but will fail if it is odd, as in your example.
If you want to store arrays as values โโin a hash, use [5, 0] instead.
You are trying to put the list as hash elements and it sees them as more key / value pairs. What you really want to do is put an array of refs like
my %data_variables = ("Sno." => [5,0], "ID" => [20,1], "DBA" => [50,2], "Address" => [80,3], "Certificate" => [170,4], ); You can refer to array elements as
my $foo = $data_variables{"Sno"}->[0]; my $bar = $data_variables{"Address"}->[1];