Justplayit
Justplayit

Reputation: 15

Odd number in anonymous hash

Anyone can explain me why this warning occurs in my program. So far my limited knowledge of perl, this should work properly.

$clone = $cromozom;
print "-- Clone: $clone->{_secventa} | $clone->{_performanta} \n";
mutatie($clone);
print "-- After mutation: $clone->{_secventa} | $clone->{_performanta} \n";
$clone->{_performanta} = performanta{$clone->{_secventa}};
$counter += 1;

And this is the performanta subroutine.

sub performanta{
    my $sir = shift;
    my $distanta = 0;
    my $index;

    for($index = 0; $index < length($sir); $index +=1){
        $distanta += abs(ord(substr($sir, $index, 1)) - ord(substr($solutieOptima, $index, 1)));
    }

   return $distanta;

}

It says Odd number of elements in anonymous hash at this line: $clone->{_performanta} = performanta{$clone->{_secventa}};.
Thank you.

Upvotes: 0

Views: 496

Answers (1)

Tanktalus
Tanktalus

Reputation: 22274

You're calling performanta{$clone->{_secventa}}. When perl tries to parse this, it's getting performanta( { $clone->{_secventa} } ) which is:

  • call the performanta sub
  • it gets one parameter
  • that one parameter is an anonymous hash ref, initialised with { ... }
  • the list that initialises that hash ref is only one item long, $clone->{_secventa}.

It's not entirely clear to me what you intended, but it's entirely clear to perl that whatever you told it isn't going to be what you intended to tell it, thus the helpful warning.

Upvotes: 1

Related Questions