Perl – Warning – “Odd number of elements in hash assignment” in perl

perl

I am getting the warning with use of following syntax –

my %data_variables = ("Sno." => (5,0),
                "ID" => (20,1), 
                "DBA" => (50,2), 
                "Address" => (80,3), 
                "Certificate" => (170,4),
            );

But I dont get a similar warning at use of similar syntax.

my %patterns = ("ID" => ("(A[0-9]{6}?)"),
                "Address" => (">([^<]*<br[^>]+>[^<]*)<br[^>]+>Phone"),
                "Phone" => ("Phone: ([^<]*)<"),
                "Certificate" => ("(Certificate [^\r\n]*)"),
                "DBA" => ("<br[^>]+>DBA: ([^<]*)<br[^>]+>"),
            );  

Best Answer

You need to change your 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 lists of numbers need to be stored as array references (hence the square brackets).

In your second example, the parentheses are superfluous and just confuse the matter. Each set of parentheses contains just one scalar value (a string), each of which becomes a hash value.

Related Topic