> I've read that > > @a = undef; > > creates an array with 1 empty element. Why does it do so? (I'm trying to > learn to think like Perl, not just learn it by rote.) > > TIA > > P.S. I'm surprised I couldn't find this in a FAQ. Doesn't everyone ask > this question once they learn this item?
Descriptions of why are probably going to vary from very technical to "it just makes sense", to "because it is Perlish", none of which are necessarily useful. This may fall into the category that Randal likes to rant (in a positive way) about the difference between "list context" and "array context". The assignment operator you show above is really just assigning to an array a "list of items". That "list of items" happens to contain one item, and it happens to be 'undef'. So the list is assigned in order starting at the first index of the array (aka 0). So to initialize an "empty" array you assign it an "empty list" which isn't a set of undef's, it is just The Empty List, @a = (); This whole list vs. array context stuff is one of the most powerful features of Perl, and probably one of the more difficult parts when coming from other languages which just aren't cool enough to allow you to do such things. HTH a little, http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>