I have run into a strange behavior in Perl that I haven't been able to find documentation for. If I (by accident) use a string as an index in an array I get the first item of the array and not undef as I would have expected.
$index = "Some string";
@array = qw(one two three);
$item = $array[$index];
print "item: " . $item;
I expected to get item: as output, but instead I get item: one. I assume that because the string doesn't start with a number it's "translated" to 0 and hence giving me the first item in the array. If the string starts with a number that part of the string seems to be used as the index.
Is this to be expected, and is there any documentation describing how strings (e.g. "2strings") are interpreted as numbers in Perl?
use strict; use warnings;in your code? Please read following webpage to get some information how a strings converted to integer (array index is an integer number).use strict;, but I have just tested withwarningsin my test code, and got some nice warnings. Please upgrade to answer.use strict;assumes that you declare variables properlymy $var = 'value';, orlocal $var = 'value';, orour $var = 'value';. Take your time to read on strict and warnings.