2011年10月10日 星期一

請勿用length 函數計算array長度($#arr+1) 計算之


)And finally, don’t use the length function to get the Perl array size. See the example code block below:


@words = qw(Perl array size);
$size = length(@words);
print "$size\n"; 


The last line will print 1 because the length function expects a scalar and the array will be consider in a scalar context – so will get the length of the length. Thus, in scalar context the notation @words means 3, and length(3) is equal to 1

陣列求長度的辦法
@arr = (1.. 10 );
$array_size = $#arr+1;
print $array_size;

延伸閱讀:

Perl Array Size






NEW!!! 

Check my new resource:    Perl How To Tutorial eBooks 







If we speak about Perl array size we mean the number of elements of a Perl array. Please note that some authors don’t make any difference between size and length, but we’ll try to be consequent and use the word "size" when we speak about the number of Perl array items. We’ll discuss below the two possible array alternatives: one-dimensional and the other multi-dimensional.

One-dimensional arrays
If you want to get the Perl array size (or the number of its elements), you can use each of the code lines shown below:


$arraySize = @array;
$arraySize = scalar (@array);
$arraySize = $#array + 1;


The first line of code uses the implicit scalar conversion, meanwhile the second line uses the explicit scalar conversion.
In the third line of code, $#array means the subscript (or index) of the last item of the array and you must add 1 in order to obtain the array number of elements.
If you want to print the Perl array size, see the code below:


print "array size = $arraySize\n";
print "array size = ", @array. "\n";
print "@array";


The first line prints the scalar variable $arraySize which contains the size of the array.
The second line prints the size of the array using directly its name.
The last line is a wrong example about how to use the name of an array to get the Perl array size. Including the array name in double quotes causes Perl to flatten the array by concatenating its elements into a string and the print function will display all the elements of the array separated by space, and not its size.
And finally, don’t use the length function to get the Perl array size. See the example code block below:


@words = qw(Perl array size);
$size = length(@words);
print "$size\n"; 


The last line will print 1 because the length function expects a scalar and the array will be consider in a scalar context – so will get the length of the length. Thus, in scalar context the notation @words means 3, and length(3) is equal to 1.


Check my new How To Tutorial eBooks (PDF format):
to see a lot of fully commented examples that help you use the Perl built-in functions and the Perl statements in your scripts.

Multi-dimensional arraysNext, we will show you how you can get the Perl array size for multi-dimensional arrays. If you remember the definition of an array, you know that an array contains a list of scalars. Because an array is not a scalar, if we include an array into another array, Perl uses the reference of the included array and not the array itself. In order to get access to an array included in another array we must dereference the array reference – we can use for this a block in curly braces, as you’ll see in the print examples shown below.


@numbersTwo = ( [1, 2, 3],  [4, 5, 6, 7], [1], 
                         [2, 5, 8, 6, 3] );
@numbersThree = ( [ [1, 3, 5, 7, 9] ],
                  [ [2, 4, 6, 8],  [22, 24], [26], 
                         [28, 30, 32] ] );


In the above example, the @numbersTwo array has two dimensions and@numbersThree array has three dimensions. Practically, the @numbersTwo array has four elements: respectively the references to the [1, 2, 3] , [4, 5, 6, 7][1] , [ 2, 5, 8, 6, 3] lists. If we want to print the numbers of elements of the @numbersTwo array (the Perl array size), we can use the code below:


print $#numbersTwo+1, "\n";


which will get us 4. If we want to print the numbers of elements of the second element of the @numbersTwo array, i.e. the list [4, 5, 6, 7], we can use the code:


print $#{$numbersTwo [1]}+1, "\n";


which will get us 4. For inner dimensions, for example if we want to print the Perl array size of the sub-array that contains the list [28, 30, 32] (included in the @numbersThree array) we can use the code below:


print $#{$numbersThree[1][3]}+1, "\n";


which will get us 3. See some other print examples below:


print $#{$numbersTwo[0]}+1, "\n";      # prints 3
print $#{$numbersTwo[1]}+1, "\n";      # prints 4
print $#{$numbersTwo[2]}+1, "\n";      # prints 1
print $#{$numbersTwo[3]}+1, "\n";      # prints 5

print $#{$numbersThree[0][0]}+1, "\n";      # prints 5
print $#{$numbersThree[1][0]}+1, "\n";      # prints 4
print $#{$numbersThree[1][1]}+1, "\n";      # prints 2
print $#{$numbersThree[1][2]}+1, "\n";      # prints 1
print $#{$numbersThree[1][3]}+1, "\n";      # prints 3


Finally, I’ll give you two samples of code about how you can print all the elements of a multi-dimensional Perl array, each subarray on a separate line:


# print @numbersTwo array

foreach $item1 (@numbersTwo){
  foreach $item2 (@{$item1}){
    print "$item2 ";  
  }
  print "\n";
}

# print @numbersThree array

foreach $item1 (@numbersThree){
  foreach $item2 (@{$item1}){
    foreach $item3 (@{$item2}){
      print "$item3 ";  
    }
    print "\n";
  }
}



Don't forget to check my new How To Tutorial eBooks (PDF format):
to see a lot of fully commented examples that help you use the Perl built-in functions and the Perl statements in your scripts.





沒有留言:

張貼留言