Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to find number of elements of an array from array reference

by chakreey (Acolyte)
on Apr 07, 2011 at 14:38 UTC ( #898078=perlquestion: print w/ replies, xml ) Need Help??
chakreey has asked for the wisdom of the Perl Monks concerning the following question:

I have a very basic query on finding number of elements in an array from array reference.

my @sss = ("1","3","4"); my $reee = []; for $entry (@sss){ push (@$reee, $entry);} my $asdad = length @$reee; print "$asdad";

Assuming, I don't know number of elements in @sss, How to find number of elements form @$reee.

Command "length @$reee" returns value 1, which is right, because it is a scalar. But I want to obtain result as 3. How to get it ?

Update: got it ! Array reference (@$ree) should be considered as array.

 my $asdad = @$ress

in line line 5 of code will give correct result !

Thanks every one

Comment on How to find number of elements of an array from array reference
Select or Download Code
Re: How to find number of elements of an array from array reference
by moritz (Cardinal) on Apr 07, 2011 at 14:41 UTC
Re: How to find number of elements of an array from array reference
by jpl (Monk) on Apr 07, 2011 at 15:15 UTC

    moritz is right. You can force an array into a scalar context using scalar(), but that is seldom necessary. For example, $asdad was assigned value 1 because the arguments to length() are in scalar context, so it is as though you had said length("3"), which is 1. If there had been 10 items in the array, you'd have a two-digit length, and $asdad would have been assigned 2.

Re: How to find number of elements of an array from array reference
by Marshall (Prior) on Apr 07, 2011 at 15:57 UTC
    Your code does not make sense.

    Assuming, I don't know number of elements in @sss
    The number of elements in @sss is just @sss in a scalar context: my $number_in_array_sss = @sss;

    my $number_in_array_sss = @sss; print scalar(@sss). " ". $num\n";
    UPDATE: more code below:
    #!/usr/bin/perl -w use strict; use Data::Dumper; my @sss = ("1","3","4"); my $reee = []; foreach my $entry (@sss) { push (@$reee, $entry); } # this is to show various uses of scalar() and that # the dot (concatenation) operator forces scalar context print "\$ree has ",scalar(@$reee)," elements\n"; print "\$ree has ".@$reee." elements\n"; print ''.@$reee." elements\n"; print "each element is: @$reee \n"; print Dumper \$reee; #PRINTS: #$ree has 3 elements #$ree has 3 elements #3 elements #each element is: 1 3 4 #$VAR1 = \[ # '1', # '3', # '4' # ]; # To accomplish the above is only one Perl statement!! # No "foreach" or "for" loop is needed, 3 lines of code # becomes one line of code. I do not count blank lines # or lines with just braces "{}" as "lines of code". my @aaa = (1,5,7); # like @sss, repeated for clarity # and using different numbers. my $bbb = [@aaa]; # this means: # allocate some memory and assign its # reference to $bbb. # The array, @aaa is copied to that # memory referenced by $bbb. print Dumper \$bbb; #prints #$VAR1 = \[ # 1, # 5, # 7 # ];
Re: How to find number of elements of an array from array reference
by Nikhil Jain (Monk) on Apr 07, 2011 at 16:16 UTC

    Finding number of elements in an array from array reference, you can get it by different ways like,

    1. As moritz said, use the array in scalar context like

    my $asdad =  @$reee; #outpur 3

    2. or, you can use scalar like,

    my $asdad =  scalar(@$reee); #outpur 3

    3. or, you can use int like

    my $asdad =  int(@$reee); #outpur 3
      ... or, you can use scalar...

      Why? Scalar assignment always imposes scalar context.

      ... you can use int like...

      Why? Scalar assignment always imposes scalar context, and an array in scalar context always evaluates to an integer.

      Make it easy on yourself!

        Why? Scalar assignment always imposes scalar context.

        Perhaps the OP didn't want to do an assignment. Contrast

        @a = ( 42 ); printf("%d\n", @a); # prints 42 printf("%d\n", scalar(@a)); # prints 1
        I find it is seldom necessary to use scalar(), since I'm more likely to do assignment or conditional testing, where scalar context is implied. But scalar() has its place when the default is list context.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-07-28 09:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (195 votes), past polls