4

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?

5
  • 1
    Do you have 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). Commented Mar 23, 2022 at 19:05
  • @PolarBear The link is exactly what I was looking for. The real code I have problems with only have use strict;, but I have just tested with warnings in my test code, and got some nice warnings. Please upgrade to answer. Commented Mar 23, 2022 at 19:37
  • use strict; assumes that you declare variables properly my $var = 'value';, or local $var = 'value';, or our $var = 'value';. Take your time to read on strict and warnings. Commented Mar 23, 2022 at 21:00
  • You can check free book Modern Perl 4 edition to learn program in Perl with modern style. Commented Mar 23, 2022 at 21:04
  • @PolarBear If you create an answer from your comments, I'll accept it. Commented Mar 24, 2022 at 14:24

2 Answers 2

6

Array index imposes numeric context. The string "Some string" in numeric context is equal to 0.

Under warnings, Perl will complain

Argument "Some string" isn't numeric in array or hash lookup at ...
Sign up to request clarification or add additional context in comments.

Comments

4

Array indexes must be integers, so non-integer values are converted to integers.

The string one produces the number 0, as well as the following warning:

Argument "Some string" isn't numeric in array or hash lookup

This concept is found throughout Perl. For the same reason, arguments to addition and multiplication will similarly be converted to numbers. And values used as hash keys will be converted to strings. Dereferencing undef scalars even produces the necessary value and reference in a process called autovivification!

$ perl -Mv5.10 -e'
   my $ref;
   say $ref // "[undef]";

   $ref->[1] = 123;
   say $ref // "[undef]";
'
[undef]
ARRAY(0x560b653ae4b8)

As you can see, an array and a reference to that array were spontaneously created in the above program because they were needed.

The lesson to take: Always use use strict; use warnings;.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.