Variable size/Get: Difference between revisions

From Rosetta Code
Content added Content deleted
(-> IDL and Tcl)
No edit summary
Line 19: Line 19:


The result means: 2 dimensions in the array, the first dimension has extent 3, the second has extent 4, the elements of the array are 2-byte integers (IDL's default for an "int"), there's a total of 12 elements in the array.
The result means: 2 dimensions in the array, the first dimension has extent 3, the second has extent 4, the elements of the array are 2-byte integers (IDL's default for an "int"), there's a total of 12 elements in the array.


==[[Perl]]==
[[Category:Perl]]
'''Interpreter:''' [[Perl]] 5.x
'''Modules:''' Devel::Size

use Devel::Size;
my $var = 9384752;
my @arr = (1, 2, 3, 4, 5, 6);
print size($var);
print total_size(@arr);


==[[Tcl]]==
==[[Tcl]]==

Revision as of 15:30, 27 February 2007

Task
Variable size/Get
You are encouraged to solve this task according to the task description, using any language you may know.

Demonstrate how to get the size of a variable.

Ada

Ada represents the size of a variable in bits, not bytes like many other languages.

Int_Bits : constant Integer := Integer'size;
Whole_Bytes : constant Integer := Int_Bits / Storage_Unit; -- Storage_Unit is the number of bits per storage element

IDL

IDL is array based, so its size() function is geared towards that:

arr = intarr(3,4)
print,size(arr)
;=> prints this:
       2           3           4           2          12

The result means: 2 dimensions in the array, the first dimension has extent 3, the second has extent 4, the elements of the array are 2-byte integers (IDL's default for an "int"), there's a total of 12 elements in the array.


Perl

Interpreter: Perl 5.x Modules: Devel::Size

use Devel::Size;
my $var = 9384752;
my @arr = (1, 2, 3, 4, 5, 6);
print size($var);
print total_size(@arr);

Tcl

Since all variables are ultimately strings in Tcl, this is easy:

string bytelength $var