Li Ngok Lam wrote: > My method sounds stupid, but still works : > > my @array = ('123', 'abc', 'def', 1..9); > my $len_of_array = 0 ; > foreach my $elem(@array) > { $len_of_array += length($elem) } > print $len_of_array ; # I got '18' > > my %hash = (1=>2, 2=>3, 3=>4); > foreach my $key(keys(%hash)) > { $len_of_hash += length($key) + length($hash{$key}) } > print $len_of_hash ; # I got '6' > > I suppose there should be another better and faster way to done this, > any suggestion ?
It really depends on what you expect to do with the result that you're getting. A Perl variable is a lot more than just its value - it includes usage data and status flags as well. Also an array has a collection of this sort of data of its own as well as each element of the array being a separate Perl scalar with separate overheads. Besides this, if you take the length of a numeric value as in the last nine elements of your @array then it will first be converted to a string before the length can be taken. Perl will hold onto both the numeric and string data as long as they are both valid in case it needs to use them again. The same value will therefore occupy a different amount of space depending on what you've done with it. If you tell us more about why you need this information then we will be able to help. Cheers, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]