Dear Monks

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:

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;
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?
Now, the scripts receives the instruction to evaluate an element of @array that has not yet been established. For example element 20.
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 } }
**************************** 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:
if (@{$x->[3]}>0) {
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:
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 (defined $x->[1]) { if (@{$x->[3]}>0) { # Do something } } }
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 tried
$array[$number] = undef,
I also tried:
delete $array[$number];
I even tried the splice function:
splice (@array,$number,1); #wipes out that element completely
followed by:
splice(@tables,$number,0,undef); #inserts a new undefined value in the + same element number
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, matematiko

In reply to Erroneous defined detection of array elements by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.