Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Finding elements common to many lists / arrays

"What do these lists have in common". That's a common question - but what does the person asking it actually mean? Is (s)he looking for items that occur in all of the lists, but perhaps at different positions? For items that occur in two or more of the lists? For items that occur at the same position in several or all of the lists? Of just to see which lists are the same length?

Questions about commonality are quite frequent in my email, and aren't always clearly thought through. And there's often a hope (which has to be dashed) from the writer that a one-liner using something like a single grep function cll will do the job. Alas, there's rather more to it than that. Even in the case of two incoming lists, each member of the first list needs to be compared to each member of the second list, and if the number of lists increases it gets all the more complex.

I've written an answer in Perl - source code [here] on our web site - that demonstrates a possible approach.

We use one of Perl's hashes to count the number of times each incoming value occurs across all the lists. And we use a second hash while we're doing so in order to avoid double-counting items which occur several times over in the same list. When the counting as been completed, we can identify all items which occur in all the lists (they have a count equal to the number of lists), items which are unique to a single list (they have a count equal to one), and so on.

Starting off with a list of pets that my we have at three houses in our street:

  @first = qw(cat dog hamster tortoise snake lizard);
  @second = qw(mouse cat dog cow horse shark);
  @third = qw(cow sheep goat dog snake cat rabbit rabbit rabbit);


We can find what's unique ... and what everyone has!

  In all lists - cat dog
  In several lists - snake cat cow dog
  In just one list - tortoise lizard horse shark rabbit hamster sheep mouse goat


Next Perl courses - start 13th December (beginners) and 20th December (advanced) - see [here] for an updated description and schedule if you're reading this in the archive.
(written 2010-11-26, updated 2010-11-27)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q110 - Object Orientation and General technical topics - Programming Algorithms
  [202] Searching for numbers - (2005-02-04)
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
  [642] How similar are two words - (2006-03-11)
  [1157] Speed Networking - a great evening and how we arranged it - (2007-04-21)
  [1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1840] Validating Credit Card Numbers - (2008-10-14)
  [1949] Nuclear Physics comes to our web site - (2008-12-17)
  [2189] Matching disparate referencing systems (MediaWiki, PHP, also Tcl) - (2009-05-19)
  [2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
  [2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21)
  [2586] And and Or illustrated by locks - (2010-01-17)
  [2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01)
  [2894] Sorting people by their names - (2010-07-29)
  [2951] Lots of way of converting 3 letter month abbreviations to numbers - (2010-09-10)
  [2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
  [3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
  [3093] How many toilet rolls - hotel inventory and useage - (2010-12-18)
  [3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
  [3451] Why would you want to use a Perl hash? - (2011-09-20)
  [3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
  [3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
  [4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
  [4401] Selecting RECENT and POPULAR news and trends for your web site users - (2015-01-19)
  [4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
  [4410] A good example of recursion - a real use in Python - (2015-02-01)
  [4652] Testing new algorithms in PHP - (2016-02-20)
  [4656] Identifying the first and last records in a sequence - (2016-02-26)
  [4707] Some gems from an introduction to Python - (2016-10-29)

P217 - Perl - More than Simple Lists and Hashes!
  [43] Hash of lists in Perl - (2004-09-09)
  [293] Course follow-ups - (2005-04-27)
  [1514] Autovivification - the magic appearance of variables in Perl - (2008-01-21)
  [2241] Perl references - $$var and \$var notations - (2009-06-15)
  [2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
  [2877] Further more advanced Perl examples - (2010-07-19)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)
  [3105] Adventure with references to lists and lists of references - (2010-12-26)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [3406] Not multidimentional arrays - but lists of lists. Much more flexible. Perl! - (2011-08-26)
  [3444] Take the dog on a lead - do not carry her. Perl references. - (2011-09-17)
  [3577] How to do multidimensional arrays (or rather lists and hashes) in Perl - (2012-01-14)
  [3906] Taking the lead, not the dog, for a walk. - (2012-10-28)

P211 - Perl - Hashes
  [240] Conventional restraints removed - (2005-03-09)
  [386] What is a callback? - (2005-07-22)
  [738] (Perl) Callbacks - what are they? - (2006-05-30)
  [930] -> , >= and => in Perl - (2006-11-18)
  [968] Perl - a list or a hash? - (2006-12-06)
  [1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
  [1705] Environment variables in Perl / use Env - (2008-07-11)
  [1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
  [1856] A few of my favourite things - (2008-10-26)
  [1917] Out of memory during array extend - Perl - (2008-12-02)
  [2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
  [2836] Perl - the duplicate key problem explained, and solutions offered - (2010-06-28)
  [2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [3106] Buckets - (2010-12-26)
  [3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)


Back to
Well House Price list, January to March 2011
Previous and next
or
Horse's mouth home
Forward to
Customer Service - the boundary
Some other Articles
Python through the Snow
Change of balance, of attitude, and of work method
Running a course ... what if the tutor isn't well?
Customer Service - the boundary
Finding elements common to many lists / arrays
Well House Price list, January to March 2011
Belfast, on a cold November evening
Strings, Garbage Collection and Variable Scope in C++
Throwing your own exception in C++, and catching it
Using C and C++ functions in the same program - how to do it
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3072_Fin ... rrays.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb