Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Perlsecret - plus no-ops

by haukex (Archbishop)
on Jun 24, 2022 at 15:46 UTC ( [id://11145018]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Perlsecret - plus no-ops (updated)
in thread Perlsecret - plus no-ops

puts the left side in scalar context

That's not quite right - the "operator" requires scalar context onfrom its LHS* for it to work correctly. From perlsecret (emphasis mine): It "provides a list context to its right side and returns the number of elements to its left side. Note that the left side must provide a scalar context; obviously, a list context on the left side will receive the empty list in the middle."

is this example the same as saying @filled = grep {scalar @$_} @arrays; ?

If by "this example" you mean the one from perlsecret, then yes, the effect is the same. ()=@$_ means to dereference the array reference that is stored in $_, and assign it to the empty list. And Assignment Operators says: "a list assignment in scalar context returns the number of elements produced by the expression on the right hand side of the assignment." The grep is what provides the scalar context to the ()=@$_ expression (as opposed to map, which provides list context for its expression).

So interpolated for the 3rd element of array this would mean grep {scalar 1,2} @array ?

No, not quite. scalar("a","b") causes the Comma Operator to be evaluated in scalar context, where "it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value." So the aforementioned expression just returns "b". This is why it's important to not use arrays of numbers when testing things like this, because scalar(1,2) returns a misleading result of 2 - scalar(9,42) returns 42. An array in scalar context returns the number of elements it contains. So a more appropriate equivalence would be something like scalar(@{["a","b"]}) instead, which evaluates to 2.

* Update shortly after posting: Well, to be nitpicky, it's not really an operator and doesn't really have an LHS. Take the example $n =()= "abababab" =~ /a/, run that through perl -MO=Deparse,-p, and you'll see something like this, which I've edited slightly for clarity: $n=( ()=( 'abababab'=~/a/ ) ). So as I explained above, what is happening is that the assignment ()=... is being evaluated in scalar context provided by the $n=....

A few other very minor edits.

Replies are listed 'Best First'.
Re^4: Perlsecret - plus no-ops
by Anonymous Monk on Jun 25, 2022 at 07:34 UTC
    thanks very much for your help, but 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? and that expression is true as processed by the comma operator,so that the [[1,2]] array ends up in @filtered?
      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://11145018]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-18 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found