abhishekv has asked for the wisdom of the Perl Monks concerning the following question:

when we are assigning any array in scalar context, we are getting length of that array. i.e. say we have an array @arr = (1,2,3,4,5,6); lets say we have a scalar $x in which i will assign this array, $x=@arr; In this case $x will contain 6 i.e the length of @arr. But I want to know the reason that when we are assigning @arr to $x , why we are getting the scalar value as the length of array ? what is the reason behind it. I searched at my level but could not find any proper reason. Can you help me please!!!! NOTE: i want to know the internal functionality for this case. Please explain in that direction only..

Replies are listed 'Best First'.
Re: array in scalar context..
by roboticus (Chancellor) on Oct 30, 2013 at 11:34 UTC

    abhishekv:

    If you read perldoc perlop under "Assignment Operators", you can see that that's how it's defined. When the designers of perl made decisions for the semantics of the action, it seems that they thought the most useful result of assigning an array to a scalar was to give the length of the array. Having used perl for a few years now, I find that the choice is a pretty useful one.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: array in scalar context..
by Happy-the-monk (Canon) on Oct 30, 2013 at 12:04 UTC

    why we are getting the scalar value as the length of array

    We don't. We get the count of all elements.

    Reason is that the designer thought that was the most helpful thing to do under the circumstances, documented it to be so, and there we are. What would your preference have been?

    If you want the combined length of all elements, use length and join to get it.

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

      LOL

      "Length of the array" is an ambiguous term. He presupposes it means number of elements, you presuppose it means data storage length.

      However, count of all elements is not ambiguous, and is a decidedly better term.

      That said, I would guess the OP's native tongue is not English, so some leeway surely can be granted here?

Re: array in scalar context
by Athanasius (Archbishop) on Oct 30, 2013 at 12:03 UTC
Re: array in scalar context..
by marinersk (Priest) on Oct 30, 2013 at 13:40 UTC
    Hello abhishekv,

    what is the reason behind it

    It's a useful function, often needed, and other languages of the era did not often provide this function built in to the language. Larry had a flash of brilliance and said -- hey, here's a cool feature, and added it to the definition of Perl.

    i want to know the internal functionality for this case

    It isn't hard to imagine. Either:

    • Perl tracks how many elements are in each array and that value is returned, or;
    • Perl doesn't track how many items are in the array, so it loops through all the entries and counts them, then returns that value.

    My money is on the former. I'd bet a whole quarter on it.

Re: array in scalar context..
by Anonymous Monk on Oct 30, 2013 at 11:46 UTC

    why we are getting the scalar value as the length of array ?

    If not the length, then what?

    NOTE: i want to know the internal functionality for this case. Please explain in that direction only..

    Can you translate this question into simple english?