Tuesday, May 14, 2013

Perl undefined array / hash


要把  array 和 hash 清掉一樣有兩種作法

@array = ();
%hash = ();

undef @array;
undef %hash;
簡單的名詞解釋:

An array is initialized or not ->
    call scalar() to check the number of elements or call defined()
    if scalar() return false (the number of elements) is 0, then the array is uninitialized.


但是以下是錯誤示範

This would fill the array with one element at index zero with a value of undefined. 
equal to $array[0] = undef;

my @array = undef;     # WRONG!!!!

No comments:

Post a Comment