Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^4: in search of a more elegant if then else

by TimToady (Parson)
on Feb 20, 2010 at 16:33 UTC ( [id://824399]=note: print w/replies, xml ) Need Help??


in reply to Re^3: in search of a more elegant if then else
in thread in search of a more elegant if then else

This gets a little prettier in Perl 6, since it parses the inside of parens as a statement:
say (if $_ { 'fred' } else { 'bill' }) for 0..1;
Also, you can usually omit the curlies on a do, if the insides can terminate the whole statement...which it can't if there's a modifier, as above, so we'll invert it:
for 0..1 { say do if $_ { 'fred' } else { 'bill' } }
Of course, there's also the ternary operator, spelled ?? !!, if you want it...

Replies are listed 'Best First'.
Re^5: in search of a more elegant if then else
by BrowserUk (Patriarch) on Feb 20, 2010 at 17:25 UTC

    I've always thought of this property of p5 if/elsif/else blocks as the blocks 'leaving behind their last value', analogous to the block of a do or sub. Whether that is a strictly accurate description of what happens internally doesn't really matter, it's just an aid memoire to my remembering the behaviour.

    I've often wished that it was property of all blocks. It could, for example, provide a solution to the problem LanX describes in global regex returning a list of arrays?. That of obtaining a (full) list of matches from a global regex.

    Something like (the non-working):

    my @filtered = grep{ ... } do{ [ $1, $2 ] while $s =~ m[(.)(.)]g };

    I've also been frustrated that I can't obtain this list directly without going through an intermediary push to an array. Presumably P6 has an answer to this already?

    I guess that a different (better) approach in P5 syntax would be another modifier on the regex (perhaps /gg).


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Yes, that's more or less a list comprehension syntax. To make it a bit more concrete, let's suppose you only want unequal chars; you could do it any of these ways in Perl 6:
      my @filtered = grep { .[0] ne .[1] }, do [ $0, $1 ] while $s ~~ m:g[(. +)(.)]; my @filtered = do [ $0, $1 ] if $0 ne $1 while $s ~~ m:g[(.)(.)]; my @filtered = ([ $0, $1 ] if $0 ne $1 for $s.comb(/(.)(.)/)); my @filtered = map -> $a, $b {[ $a, $b ] if $a ne $b }, $s.comb;
      We get list comprehensions more or less for free in Perl 6 because loops are basically just maps in disguise, and because we allow conditional modifiers inside of looping modifiers. That modifier nesting is something that Perl 5 could easily steal back from Perl 6, even if the loop doesn't automatically return its values.

        Out of interest, do you have a preference amongst those 4?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-03-29 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found