RE: How to measure Array or Hash's byte size ?
Vincent Bufferne wrote: What's about: my @foo = ( '1', '2' ,'3' ); my $size = $#foo + 1; print table size $size\n; Ouput: table size 3 print Table size: , scalar(@foo), \n; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: How to measure Array or Hash's byte size ?
What's about: my @foo = ( '1', '2' ,'3' ); my $size = $#foo + 1; print table size $size\n; Ouput: table size 3 Vincent -Original Message- From: Li Ngok Lam [mailto:[EMAIL PROTECTED] Sent: Thursday, April 03, 2003 5:14 PM To: [EMAIL PROTECTED] Subject: How to measure Array or Hash's byte size ? 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 ? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to measure Array or Hash's byte size ?
What's about: my @foo = ( '1', '2' ,'3' ); my $size = $#foo + 1; you can simplify this by : $size = scalar @foo; print table size $size\n; Ouput: table size 3 TIA, but this is not what I want... because each element is assumpted not in same length... ie.. I am not going to get the table size... Vincent -Original Message- From: Li Ngok Lam [mailto:[EMAIL PROTECTED] Sent: Thursday, April 03, 2003 5:14 PM To: [EMAIL PROTECTED] Subject: How to measure Array or Hash's byte size ? 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 ? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to measure Array or Hash's byte size ?
Li Ngok Lam wrote: What's about: my @foo = ( '1', '2' ,'3' ); my $size = $#foo + 1; you can simplify this by : $size = scalar @foo; You can simplify even this by : $size = @foo :) Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: How to measure Array or Hash's byte size ?
That's not what he was asking. On Thu, 2003-04-03 at 10:07, BUFFERNE,VINCENT (HP-France,ex1) wrote: What's about: my @foo = ( '1', '2' ,'3' ); my $size = $#foo + 1; print table size $size\n; Ouput: table size 3 Vincent -Original Message- From: Li Ngok Lam [mailto:[EMAIL PROTECTED] Sent: Thursday, April 03, 2003 5:14 PM To: [EMAIL PROTECTED] Subject: How to measure Array or Hash's byte size ? 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 ? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
How to measure Array or Hash's byte size ?
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 ?
Re: How to measure Array or Hash's byte size ?
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]
Re: How to measure Array or Hash's byte size ?
Rob Dixon said: 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. Rob is correct, but the following code is another way to get the results you had: local $ = ; print length @array; my @tmp = %hash; print length @tmp; However, I don't think there's anything particularly wrong with your code, which can't be said for mine. -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to measure Array or Hash's byte size ?
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]
Re: How to measure Array or Hash's byte size ?
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 if just shorter, I guess I will do in this way : print length ( join , @array); print length ( join , %hash); But I am not sure if this will eat up (much) more of my memory -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to measure Array or Hash's byte size ?
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 ? This probably does not do what your subject line indicates. Those are not bytes it is measuring; they are characters. My advice is to not try this in Perl, because it misses the point. Perl is not C. It has a radically different paradigm regarding physical storage of data. We do not, and should not, have access to the physical storage details. We ask the system to store a scalar--a logical, not physical, value--and link it to our chosen symbol, and trust the system to get the job done. We can then call upon that logical value by addressing the symbol. AFAIK, this is all we can really know about the storage of our data. Of course if you want to start with: perldoc perlguts and see where it leads you, you may find enough information to generate an accurate picture of the memory model. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]