Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > size of anonymous array

Reply
Thread Tools

size of anonymous array

 
 
jtbutler78@comcast.net
Guest
Posts: n/a
 
      06-14-2006
I have a hash with the values being arrays. How can I find the size of
these arrays?

example:
$hash{$key} = [ 0, 1];
$hash{$key1} = [ 0, 1];
$hash{$key2} = [ 0, 1,2];
$hash{$key3} = [ 0];


thanks

 
Reply With Quote
 
 
 
 
jtbutler78@comcast.net
Guest
Posts: n/a
 
      06-14-2006
Cool thanks.

What about just say I wanted the size of $hash{baz} => [ 0,1,2 ] ?


(E-Mail Removed)-here.net wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> writes:
> > I have a hash with the values being arrays. How can I find the size of
> > these arrays?
> >
> > example:
> > $hash{$key} = [ 0, 1];
> > $hash{$key1} = [ 0, 1];
> > $hash{$key2} = [ 0, 1,2];
> > $hash{$key3} = [ 0];
> >

>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Data:umper;
>
> my %hash = ( foo => [ 0,1 ] ,
> bar => [ 0,1 ] ,
> baz => [ 0,1,2 ],
> waldo => [ 0] );
>
> my $sum;
> $sum += @$_ for values %hash;
>
> print "total length = $sum\n";
>
> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> Lawrence Statton - http://www.velocityreviews.com/forums/(E-Mail Removed) s/aba/c/g
> Computer software consists of only two components: ones and
> zeros, in roughly equal proportions. All that is required is to
> sort them into the correct order.


 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      06-14-2006

(E-Mail Removed) wrote:
> I have a hash with the values being arrays. How can I find the size of
> these arrays?
>
> example:
> $hash{$key} = [ 0, 1];
> $hash{$key1} = [ 0, 1];
> $hash{$key2} = [ 0, 1,2];
> $hash{$key3} = [ 0];



use strict;
use warnings;

my %hash = (
one => [0],
two => [1,2],
three => [4,5,6],
);

map { my $size = @{$hash{$_}}; print "$_ => $size\n" } keys %hash;

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      06-14-2006
http://www.velocityreviews.com/forums/(E-Mail Removed)-here.net wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> writes:
>
> > What about just say I wanted the size of $hash{baz} => [ 0,1,2 ] ?

>
> Any list evaluated in scalar context returns its size.


No. Any *array* evaluated in scalar context returns its size. A
"list" in scalar context doesn't exist. The comma-operator in scalar
context returns its right-most argument.

> @$hash{baz} is the list ( 0, 1, 2 )


No, it's not. @$hash{baz} is the same as @{$hash}{baz}, which is the
one-element slice of the hash %{$hash} containing the element 'baz'.
I'm guessing you meant @{$hash{$baz}}, which is the array referenced by
the hash value $hash{$baz}.

You need to re-read the dereferencing rules in
perldoc perlref

Even so, that would be the *array* containing the list (0, 1, 2). The
distinction is important. Please read:
perldoc -q difference

> Therefore
>
> my $size = @$hash{baz};


my $size = @{$hash{baz}};

> You can use scalar() to force scalar context in places where that is
> needful.
>
> print 'The array has ', scalar(@$hash{baz}), ' element(s)';


Or just use an operator that does force scalar context, instead of one
that gives list context:

print 'The array has ' . @{$hash{baz}} . ' element(s)';

Paul Lalli

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      06-14-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:
> I have a hash with the values being arrays. How can I find the size of
> these arrays?


> example:
> $hash{$key} = [ 0, 1];



But using "Use Rule 1" from:

perldoc perlreftut


I like to do it in 3 steps:

my $size = @array; # pretend it is a plain array

my $size = @{ }; # replace the array's *name* with a block

my $size = @{ $hash{$key} }; # put something in the block that
# returns the right type of reference


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
Is this a local anonymous class or a member anonymous class Reporter Java 3 05-12-2007 05:23 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
Could a struct with size 44 bytes point always points to a char array with size 2048 bytes? eagle_jyjh@citiz.net C Programming 5 04-09-2006 02:49 PM
help with an anonymous array of anonymous hashes noeldamonmiller@gmail.com Perl Misc 1 02-10-2005 01:08 AM



Advertisments