size of anonymous array

Discussion in 'Perl Misc' started by jtbutler78@comcast.net, Jun 14, 2006.

  1. Guest

    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
     
    , Jun 14, 2006
    #1
    1. Advertising

  2. Guest

    Cool thanks.

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


    -here.net wrote:
    > "" <> 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::Dumper;
    >
    > 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 - 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.
     
    , Jun 14, 2006
    #2
    1. Advertising

  3. 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;
     
    it_says_BALLS_on_your forehead, Jun 14, 2006
    #3
  4. Paul Lalli Guest

    -here.net wrote:
    > "" <> 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
     
    Paul Lalli, Jun 14, 2006
    #4
  5. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 14, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    559
    Flash Gordon
    Apr 9, 2006
  2. Replies:
    8
    Views:
    512
    Bob Hairgrove
    Apr 10, 2006
  3. Reporter
    Replies:
    3
    Views:
    485
    Mike Schilling
    May 12, 2007
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,655
    Michael Jung
    May 25, 2008
  5. Replies:
    1
    Views:
    228
Loading...

Share This Page