It seems to me the other comments are answering a different question than the one you are asking. Or maybe I'm the one misunderstanding your question.

Let's say you have an array which you initialize with some values:

@values = qw( 00 11 22 33 ); use Data::Dumper; print Dumper \@values; # generates the output: $VAR1 = [ '00', '11', '22', '33' ];

By the way, I'm cutting and pasting from my debugging session perl -d -e 1. That means I may forget the semi colon at the end of a line, you don't need them in the debugger.

So the Array contains four values: $values[0] is "00", up to $values[3] which is "33".

Let's suddenly create $values[6]. What do you suppose happens with the in-between array slots, for which we have not defined values? What happens with $values[4] and $values[5]?

$values[6] = "66"; print Dumper \@values; # generates the output: $VAR1 = [ '00', '11', '22', '33', undef, ${\$VAR1->[4]}, '66' ];

The first four elements are unchanged. The fifth element, $values[4], is undefined. So is the sixth element, $values[5] ... Data::Dumper uses funny notation to say it has the same value as $values[4], in this case being undefined.

Some offices have a set of cubbyholes where the mail is put. This array is like one of those sets of cubbyholes. The first four cubbyholes contain pieces of paper with the text, "00", "11", "22", "33". The next two cubbyholes are empty. They don't contain pieces of paper, but the cubbyholes exist. Pieces of paper could be placed in them, in which case they would become defined. The last cubbyhole contains a value, like the first four.

Lets change things, so the every other cubbyhole has a value, and the others don't.

The fifth array slot needs to be defined:

$values[4] = "44"; print Dumper \@values; # generates the output $VAR1 = [ '00', '11', '22', '33', '44', undef, '66' ];

Now there are some elements we want to get rid off. You can't assign 0, because that's a value. So is '', that's an empty string, same as "". Even assigning the value undef doesn't work!

$values[1] = undef; print exists $values[1]; # generates the output: 1, i.e., the array slot contains the value 'un +def'.

The only way to make the array element have no value is to delete the value:

delete $values[1]; delete $values[3]; print Dumper \@values; # generates the output: $VAR1 = [ '00', undef, '22', ${\$VAR1->[1]}, '44', ${\$VAR1->[1]}, '66' ];

As you can see, four elements have values, the other three are undefined.This is what it means to delete elements from an array.


Note the subtle difference between an array element which is not defined, and an array element which is defined to contain the value undef.

$values[6] = undef print Dumper \@values; # generates the output : $VAR1 = [ '00', undef, '22', ${\$VAR1->[1]}, '44', ${\$VAR1->[1]}, undef ];

Notice that $values[6] does not use the funny Data::Dumper notation to say it is identical to elements 1, 3, and 5. That's because those three are undefined, and this one is defined. Let's continue assigning undef.

$values[0] = undef; $values[2] = undef; $values[4] = undef; print Dumper \@values; # generates the output : $VAR1 = [ undef, undef, undef, ${\$VAR1->[1]}, undef, ${\$VAR1->[1]}, undef ]; $values[1] = undef $values[3] = undef $values[5] = undef print join( "\n", \$values[0], \$values[1], \$values[2], + \$values[3], \$values[4], \$values[5], \$values[6] ); # generates the output : SCALAR(0x8279dd4) SCALAR(0x81d64e0) SCALAR(0x8279d50) SCALAR(0x81d6294) SCALAR(0x8279d38) SCALAR(0x81d627c) SCALAR(0x8279e10)

See? Each array element contains a separate scalar, each with the value, undef. On the other hand, deleting each element has a different effect:

delete @values[0]; delete @values[1]; delete @values[2]; delete @values[3]; delete @values[4]; delete @values[5]; delete @values[6]; print Dumper \@values; # generates the output : $VAR1 = [];

Once you delete all the elements, even the array slots disappear! Of course, the more efficient way to empty the array is to assign an empty list:

@values = ();

It may be useful to also read the system documentation on delete. Type perldoc -f delete


This started off as a quick and easy clarification. Why is it now two hours later?

--
TTTATCGGTCGTTATATAGATGTTTGCA


In reply to What it means to define and to delete array elements by TomDLux
in thread Removing null values from within an array by coldfingertips

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.