Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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 ( [id://898106]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^2: How to find number of elements of an array from array reference
by chromatic (Archbishop) on Apr 07, 2011 at 17:30 UTC
    ... 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.

        I prefer 0 + @a in that case, to be explicit about numeric scalar context, but point taken.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://898106]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-18 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found