Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How do you determine the size of array reference?

by apl (Monsignor)
on Jul 02, 2007 at 15:33 UTC ( #624484=perlquestion: print w/ replies, xml ) Need Help??
apl has asked for the wisdom of the Perl Monks concerning the following question:

Brother Monks, I've Super Searched, I've Googled, and I've played with the smallest possible program (below) I could build, but I haven't been able to find how to deterime the size of a reference to an array (not a hash)
use strict; my @Header = ( 'Record Type ', 'File Generation Date/Time ', 'Origin ', ); my @New; my @Trailer; my %Classes = ( 'NW' => { labels => \@New }, 'HD' => { labels => \@Header }, 'TR' => { labels => \@Trailer }, ); my $type = 'HD'; Display( $Classes{ $type }->{labels} ); sub Display { my ( $Labels ) = @_; my $count = scalar( $Labels ); ### I know this doesn't work. print sprintf( "Display: %d '%s'\n", $count, $Labels->[1] ); }
I realize if I was using an array (rather than an array reference) I could use $#name or scalar( @Name ).

Unfortunately, I need to use the contents of @Header sequentially, so using a hash isn't an option.

Thank you for your help in advance.

Comment on How do you determine the size of array reference?
Select or Download Code
Re: How do you determine the size of array reference?
by mikeB (Friar) on Jul 02, 2007 at 15:39 UTC
    Try perldoc perlreftut for all your reference (and dereferencing) needs.
      Many thanks. I'll read it this afternoon...
Re: How do you determine the size of array reference?
by Fletch (Chancellor) on Jul 02, 2007 at 15:39 UTC

    scalar @{ $arrayref }

    Pretty much wherever you use @array you can substitute in @{$arrayref} (and the related invocation for the last index of the array ref is $#{ $arrayref }, but you really shouldn't be using that to determine the size).

      Thanks, Fletch. That echo you hear is me slapping my forehead...
Re: How do you determine the size of array reference?
by naikonta (Curate) on Jul 02, 2007 at 15:50 UTC
    I've Super Searched, I've Googled
    Really? Try this: array size, or determine array size in perl.

    But, what do you really mean by array size? The number of elements?

    my @array = (1 ..4); my $size = @array; print "The size of array \@array is $size\n";
    The memory size?
    use Devel::Size 'size'; my @array = (1 ..4); my $size = size(\@array); print "The size of array \@array is $size\n";
    Should you used an array reference, then you could do the same.
    my @array = (1 ..4); my $ar_ref = \@array; my $size = @$ar_ref; print "The size of array \@array is $size\n"; use Devel::Size 'size'; my $memory = size($ar_ref); print "The size of array \@array is $memory\n";

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      Many thanks, naikonta. My queries were along the lines of Perl array reference size. reference probably made it too specific, while size (in 20-20 hindsight) should have been something like number of elements.
      IAE, I thank you for your time.
Re: How do you determine the size of array reference?
by FunkyMonk (Canon) on Jul 02, 2007 at 16:08 UTC
    This...

    I realize if I was using an array (rather than an array reference) I could use $#name or scalar( @Name ).
    ...makes me think you want the number of elements in a reference to an array. You need to convert your arrayref to an array, like so

    my $arrayref = [ 1, 2, 3 ]; #step-by-step my @array = @$arrayref; # derefernce to array, @{ $arrayref } works to +o my $num_elements = @array; #in one step $num_elements = @$arrayref; #or, when in list context print scalar @$arrayref; #or, if it's the last element number you want print $#$arrayref;
      Thank you for your time and your patience, FunkyMonk. That's exactly what I needed to know.
Re: How do you determine the size of array reference?
by leocharre (Priest) on Jul 02, 2007 at 16:23 UTC
    my $ar1 = [qw(this is another element)]; my $ar2 = [ 'this','is','another','element']; my @a1 = ('this','is','another','element'); my @a2 = (qw(this is another element)); printf " \$ar1 has %s elements \$ar2 has %s elements \@a1 has %s elements \@a2 has %s elements ", scalar @$ar1, scalar @{$ar2}, scalar @a1, scalar @a2;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://624484]
Approved by naikonta
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2014-09-17 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (100 votes), past polls