Search code examples
arraysperlloopsoctal

Check for octal duplicates in array elements Perl


I have this loop that checks duplicate elements in an array. The problem is once the array has octal numbers the compiler throws an error. How do i make this work?

my @array1 = (010895401, 010895401, 010895402, 010895403); 

my %seen;

foreach my $octal (@array1)
{
    next unless $seen{$octal}++;
    print "'$octal' is duplicated.\n"
}

Solution

  • It throws this error Illegal octal digit '8' and '9' at main.pl line 1, at end of line

    A literal starting with 0 (other than those starting with 0x or 0.) is expected to be the octal representation of a number. The octal representation of numbers consists of digits 0-7 exclusively (similar to how the binary representation of numbers consists of the digits 0 and 1 exclusively).

    As such, 010895401 is expected to be the octal representation of a number, but it isn't. 010895401 makes no sense.