Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Logical/defined or as lvalue

by ikegami (Patriarch)
on Sep 03, 2024 at 19:43 UTC ( [id://11161548]=note: print w/replies, xml ) Need Help??


in reply to Logical/defined or as lvalue

Is ( X || Y ) = Z; really something we want to see in our code? When would it be useful?

Replies are listed 'Best First'.
Re^2: Logical/defined or as lvalue
by NERDVANA (Priest) on Sep 03, 2024 at 21:13 UTC
    We have ($w? $x : $y)= $z so why not?
      Well yes, according to Wikipedia is PHP implementing the "Elvis operator"
      • $x ?: $y
      as
      • $x ? $x : $y

      But I think Perl has bigger fish to frie.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        My point was that if the ternary conditional is an lvalue, then I don't see why not to make the '||' and '//' operators lvalues. It would result in better consistency.
        ... implementing the "Elvis operator" ... as $x ? $x : $y

        Actually, I read the opposite from the Wiki article:

        ... the Elvis operator expression A ?: B is approximately equivalent to the ternary conditional expression A ? A : B

        ... something like ... x = f() ?: g() ... is equivalent to ... the conditional ternary operator x = f() ? f() : g() except that it does not evaluate f() twice if it yields truthy.

        Accentuations by me.

        Greetings,
        🐻

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

      Many reasons why not. I already gave the two primary ones: lack of readability (due to its rare and unexpected use) and lack of usefulness.

      If anything, I read your post as an argument against ( W ? X : Y ) = Z. But that has at least some modicum of usefulness.

Re^2: Logical/defined or as lvalue
by jo37 (Curate) on Sep 03, 2024 at 20:12 UTC
    When would it be useful?

    Consider items from @list to be counted, with one "given" set and a "rest". A pre-initialized hash %h1 holds the "given" set and an empty hash %h2 counts the rest. Then we could:

    ${\($h1{$_} // ($h2{$_} //= 0))}++ for @list;

    Greetings,
    🐻

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

      If you're going to count them all, I don't see the point of having the counts split across two hashes. If there's an important distinction between the two sets of strings, you would need those lists, and you could access the counts of either lists.

      If I wanted to do this, I'd count only in one hash and use a hash-slice to extract the "given" ones.

      Less code and more flexible.

      Update

      Alternatively a HoH where the first key decides what is given or rest.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2026-03-05 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.