Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^5: Perlsecret - plus no-ops

by haukex (Archbishop)
on Jun 25, 2022 at 07:53 UTC ( [id://11145032]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Perlsecret - plus no-ops
in thread Perlsecret - plus no-ops

I think that I'm missing something fundamental here... isn't for the third element of @array grep {@$_} @arrays yielding grep { 1,2 } @arrays when dereferenced?

I think perhaps the fundamental thing is that in Perl, arrays are different from lists. Lists and the comma operator are one way of several to initialize an array, as in my @array = ("a","b","c");, but once those items are stored in the array, the comma operator is no longer relevant; @array is not the same as the literal expression ("a","b","c") any more, including for dereferenced array references. An array in scalar context returns the number of elements it contains, and grep provides scalar context to its expression, so grep {@$_} @arrays is equivalent to grep {scalar(@$_)} @arrays, and scalar(@$_) yields 2 for the element [1, 2]. That's not because 2 is the last item in the list, but because there are 2 items in the listarray.

In the example code, try replacing @arrays = ( [], [1], [ 1, 2 ], [], [ 5 .. 9 ] ); with @arrays = ( [], ["a"], ["b", "c"], [], ["d".."h"] );, and the functionality will be the same. Then, try replacing it with @arrays = ( [], [0], [0, 0], [], [0,0,0,0,0] ); (a bunch of false values) and it will still work the same. scalar(0,0,0) is false, because it returns the last value of the list, 0, which is false, but my @x=(0,0,0); scalar(@x) is true, because the array @x evaluates to 3. That's how the grep works here.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-25 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found