I am getting this error -> "Can't use 'defined(@array)' (Maybe you should just omit the defined()?)"
On this line of code:
if ( defined( @{ $linkbot{"$nroboton"} } ) ) {
How can i fix this?
I am getting this error -> "Can't use 'defined(@array)' (Maybe you should just omit the defined()?)"
On this line of code:
if ( defined( @{ $linkbot{"$nroboton"} } ) ) {
How can i fix this?
defined tests whether a scalar value is undef, therefore it's nonsensical on an array. You can test whether the scalar is defined before using it as an arrayref, or if you're trying to test if the array is empty, just removed the defined() as the error message says.
# if this hash value is defined
if (defined $linkbot{$nroboton}) {
# if this referenced array has elements
if (@{ $linkbot{$nroboton} }) {
Use define
on the variable $nroboton
itself, and/or if (@{$linkbot{$nroboton}})
for the anonymous array whose reference is the value for that key, as explained.
Once you need to check on any of that it stands to reason that you may well also need to test whether there is a key $nroboton
in the hash %linkbot
, with exists
if ( exists $linkbot{$nroboton} ) { ... } # warning if $nroboton undef
so altogether
if (defined $nroboton and exists $linkbot{$nroboton}) { ... }
and now you can check and work with the data in the arrayref, @{$linkbot{$nroboton}}
.
Note that there is no need to double-quote that variable; it will be evaluated.