펄(perl)에서 array의 length(size)을 구하기
scalar(@whatever)
출처 : http://www.goldfish.org/books/O'Reilly%20Perl%20CD%20Bookshelf%202.0/prog/ch02_08.htm
2.8.2. Array Length
You may find the number of elements in the array @days by evaluating @days in a scalar context, such as:
@days + 0; # implicitly force @days into a scalar context scalar(@days) # explicitly force @days into a scalar context
Note that this only works for arrays. It does not work for list values in general. As we mentioned earlier, a comma-separated list evaluated in scalar context returns the last value, like the C comma operator. But because you almost never actually need to know the length of a list in Perl, this is not a problem.
Closely related to the scalar evaluation of @days is $#days. This will return the subscript of the last element of the array, or one less than the length, since there is (ordinarily) a 0th element. Assigning to $#days changes the length of the array. Shortening an array by this method destroys intervening values. You can gain some measure of efficiency by pre-extending an array that is going to get big. (You can also extend an array by assigning to an element beyond the end of the array.) You can truncate an array down to nothing by assigning the null list () to it. The following two statements are equivalent:
@whatever = (); $#whatever = -1;
And the following is always true:
scalar(@whatever) == $#whatever + 1;
Truncating an array does not recover its memory. You have to undef(@whatever) to free its memory back to your process's memory pool. You probably can't free it all the way back to your system's memory pool, because few operating systems support this.
'Tool' 카테고리의 다른 글
의학도를 위한 SKYSCAPE (0) | 2009.06.05 |
---|---|
Slick run, Pstart - 즐겨찾기 및 메모 및 단축키 프로그램 소개 (0) | 2008.06.21 |
KTF 도시락플레이어 MP3 전송하기 (0) | 2008.04.16 |
php의 log4php의 appender 설명 (0) | 2008.01.18 |
ie7pro (0) | 2008.01.14 |