Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Merge arrays - seeking slicker approach

by roboticus (Chancellor)
on Dec 21, 2010 at 15:12 UTC ( [id://878278]=note: print w/replies, xml ) Need Help??


in reply to Merge arrays - seeking slicker approach

puterboy:

I don't know of a way to do so, but List::MoreUtils has a handy function uniq to handle the first case:

#!/usr/bin/perl use strict; use warnings; use List::MoreUtils qw(uniq); my @a = qw(alpha beta gamma); my @b = qw(delta kappa gamma); my @c = uniq(@a, @b); print join(", ", @c), "\n";

You'll also find some interesting items in List::Util, Hash::Util and Scalar::Util.

But if you find the temporary a distraction and don't want to include a module for just that purpose, just hide it in a subroutine and go ahead and use the temporary in your subroutine. After all, a temporary isn't necessarily inefficient. So by writing a subroutine with a good name, you can make the code cleaner and self-documenting at the same time, as in the above example. I find that some code can be difficult to read when there's "too much action" happening in a statement, and you may find it difficult to maintain if the statement is too hard to create. So just relax, write a subroutine to do your dirty work, and use it--or include an appropriate module, where someone has already done the dirty work for you!

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found