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 ?


I don't know if it is "better" or faster, but it is shorter.  :-)

$ perl -le'
my @array = ( "123", "abc", "def", 1 .. 9 );
my $len_of_array = do { local $"; length "@array" };
print $len_of_array;
'
18

$ perl -le'
my %hash = ( 1 => 2, 2 => 3, 3 => 4 );
my $len_of_hash = do { local $"; length "@{[%hash]}" };
print $len_of_hash;
'
6



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to