Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: 3 strings to join

by BlaisePascal (Monk)
on Sep 29, 2000 at 02:39 UTC ( #34525=note: print w/replies, xml ) Need Help??


in reply to 3 strings to join

From the "gratuitous use of grep" club...
$string = "$a where " . join ' and ',grep {$_} ($b,$c);
Explanation: Read this from right-to-left. The grep takes as arguments a block of code (or a sub) and a list of items and return just the items in the list which the block of code is true for. In this case, the block of code is simply $_, so grep returns all items in the list ($b,$c) which are true (not undef, '', 0, or '0'). The join then takes those true items, and combines that list with ' and '. If there only one item, join omits the ' and '. It then concatenates the resulting string of 'where' clauses to the string formed by "$a where ", to get the resulting string.

Replies are listed 'Best First'.
RE:(even more grep:)3 strings to join
by jptxs (Curate) on Sep 29, 2000 at 02:58 UTC

    nice.

    throw in another join and grep:

    $string = join ' where ', grep {$_} ($first, join ' and ', grep {$_} ( + $b, $c ));
    and it can even handle the case where there are no where clauses...

    -- I'm a solipsist, and so is everyone else. (think about it)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://34525]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2018-07-22 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (452 votes). Check out past polls.

    Notices?