Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: TIMTOWTDI and other languages

by broquaint (Abbot)
on Apr 28, 2002 at 22:45 UTC ( #162724=note: print w/ replies, xml ) Need Help??


in reply to TIMTOWTDI and other languages

Here's a ruby example

alphas = [9, 4, 3, 2, 22, 13, 7, 140, 95, 278] betas = [8, 3, 4, 1, 278, 94, 15, 7, 19, 200] result = (alphas & betas).sort
There's a quite a few other ways to do it too, but I liked that one the most.

Update: Noticing a somewhat lack of variety in the languages used here's a python example too

alphas = [9, 4, 3, 2, 22, 13, 7, 140, 95, 278] betas = [8, 3, 4, 1, 278, 94, 15, 7, 19, 200] result = [] for x in alphas: if x in betas: result.append(x) result.sort()
This is probably the more obvious path, but if you like your tools functional ...
result = filter(lambda y: y != 0, [x in betas and x for x in alphas]) result.sort()
The closest I can get in perl is this (map == list comprehension?)
@result = sort { $a <=> $b } map { my $x = $_; grep $x == $_, @betas } @alphas;

HTH

_________
broquaint

update: added python examples
update2: added perl version of last python example


Comment on Re: TIMTOWTDI and other languages
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-07-13 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (247 votes), past polls