Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks.i have a error that i dont understand what is it? please help me.

#!/usr/bin/perl -w use strict; my @AoA; open (IN,'<result'); while(<IN>){ my @chunks = split; push @AoA, [ @chunks[1..$#chunks]]; } my %neighbours; for my $domain (@AoA) { for my $idx (0 .. $#$domain) { my @near = grep {$_ >= 0 && $_ <= $#$domain} $idx - 1, $idx + +1; push @{$neighbours{$domain->[$idx]}}, map {$domain->[$_]} @nea +r; } } open (DOMAINLIST,'<domainlist') or die "cannot open 'domainlist'becuas +e:$!"; chomp(my @domain=<DOMAINLIST>); for my $domain(@domain){ my @array=@{$neighbours{$domain}}; @array=removeduplicates(@array); my $number=scalar @array; print "Domain $domain.......$number\n"; } sub removeduplicates{ my @array=@_; my %seen; $seen{$_}++ for @array; my @unique = keys %seen; return @unique; }
the error is :cant use an undefined value as an ARRAY reference in line 24.

Replies are listed 'Best First'.
Re: array references
by kennethk (Abbot) on Jul 29, 2011 at 18:48 UTC
    Well, line 24 is:

    my @array=@{$neighbours{$domain}};

    As @{...} is an array dereference, it would appear that $neighbours{$domain} is undefined. I can't tell more without seeing your input (and desired output), but you could at least bandage this with something like:

    for my $domain(@domain){ next unless exists $neighbours{$domain}; my @array=@{$neighbours{$domain}};

    Of course, this explicitly skips any element of @domain where %neighbours is uninitialized, which may be problematic for your case. You might also consider something like:

    for my $domain(@domain){ my @array=exists $neighbours{$domain} ? @{$neighbours{$domain}} : +();

    where this second option silently swallows domain misses. See perlreftut for more on references.

      thank you for your help, but it seams the first element in @domain doesnt come to process.

        I do not understand what you mean - see How do I post a question effectively?. Specifically, if your code is not behaving as expected, you should post sample input and expected output (both wrapped in <code> tags) so that we can replicate your issue.

        A good bug report is worth a thousand words.