My perl script makes use of an usual complicated array (or at least unusual for me) with all sorts of different elements.
Let's say the array is called @array which it starts getting populated not in index-numerical order thru the
push (@array,$element) instructions,
but rather thru the $array[$number]=$element where $number can take anyvalue between 1 and 120 and $element has an structure like this:
Now, lets say the element 70 of @array is at this moment the only one that has been established. Am I to assume that elements 0-69 and 71-120 are undefined?my $number=70 # I am just using 70 as an example my $element=[ $justanumber, # Just a number \%values, # This is hash previously populated ['',undef,[]], [], ]; $array[$number]=$element;
**************************** My problem is that not defined ($array[$number] returns false and the subroutine is not halted as it should because elment number 20 of @array was never declared/establish/define. Then when it comes to evaluate the line:sub asubroutine { $my number=shift ; # $number takes the value of 20 if ( not defined ($array[$number])) { print "There is no such element $number in the array.\n"; return; } my $x=$array[$number]; if (@{$x->[3]}>0) { # Do something } }
I get the error: Can't use an undefined value as an ARRAY reference at script.pl xxxx I overcome the problem by adding an extra conditional:if (@{$x->[3]}>0) {
So, I don't think I had my problem fixed, but rather just patched. What I am looking for is a better acceptable solution based on facts and perhaps some enlightment into why the script was not working when it was suposed to. Why not defined ($array[$number]) returns false? If that particular element is not defined. This problem also hapens when an element of the array has been established/define at least once and later undef. I have triedsub asubroutine { $my number=shift ; # $number takes the value of 20 if ( not defined ($array[$number])) { print "There is no such element $number in the array.\n"; return; } my $x=$array[$number]; if (defined $x->[1]) { if (@{$x->[3]}>0) { # Do something } } }
I also tried:$array[$number] = undef,
I even tried the splice function:delete $array[$number];
followed by:splice (@array,$number,1); #wipes out that element completely
And still, same behaivior. My humble opinion is that due to the complexity of the element and perhaps some perl bug or limitation the element never gets wiped out or undefined completly, assuming I am allowed to say that besides 'undefined' and 'defined' the term 'kinda-undefined' or 'kinda-defined' are also a fact in perl. By the way, I am in Windows 7 32 bits running ActivePerl 5.10 Thanks in advance, matematikosplice(@tables,$number,0,undef); #inserts a new undefined value in the + same element number
In reply to Erroneous defined detection of array elements by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |