Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^3: how to find combine common elements of an array?

by jaredor (Priest)
on Jun 23, 2014 at 05:03 UTC ( [id://1090883] : note . print w/replies, xml ) Need Help??

in reply to Re^2: how to find combine common elements of an array?
in thread how to find combine common elements of an array?

You are too kind Miller, and you flatter me to think that my one-off is worth remembering!

Such kindness deserves a little extra effort, and so here is a little defensive coding to go along with the main thrust of the code: If you are not sure that there will be no duplication of numbers in any given list, then in general you will not be able to collapse the list to a minimal set of elements. For example, if you just have a single list, '3 3 4', then the code will return a list of '3 3 4', not '3 4'. (However, if the currently duplicated element has been seen before, there will be no duplication.) This potential problem is countered by another uniq statement just before the assignment to @v and is (to my taste) slightly ugly, but necessary if you want to run this on slightly muddier data sets.

You must be right about the title to this post being too obscure, since I presume you are one of only the six who upvoted this post. With 225 XP left to go, my long, hard slog to curate will only happen by me personally upvoting other posts in "Recent Threads." ;-)

Thank you again for thinking of this post. Double thanks for crediting me elsewhere!


P.S. I've only run the code through my somewhat fallible wet-ware, so caveat coder. One of these days I'll be an active Perl coder again and I won't make as many educated guesses as I do now. (Project Euler was my perl outlet for a while, but that site is down now. Gee, I can't recall, did I use the same password for PM as for PE? I hope it wasn't the other way around before, say, May 20th, 2009 ;-)

P.P.S. EDIT Oops, this was supposed to be a reply to wind.

  • Comment on Re^3: how to find combine common elements of an array?