Home Accessibility Courses Diary The Mouth Facebook Resources Site Map About Us Contact
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
Intelligent Matching in Perl

Is 10 greater than 8?

As a number, yes it is ... but if you're comparing character strings and you would for a book's index, then the character "1" comes before the character "8", so in this context the 10 is earlier than (i.e. less than) 8.

Is 14.0 the same as 14?

As a numeric value, yes it is. As a string of characters, no, it isn't.

Language authors have had the conundrum of how to make comparisons such as these since computer programming started. Does "equal" mean "identical", or does it mean "having the same value as", or even "occupying the same memory location as" or just "looks like". When he wrote Perl, Larry Wall provided a range of comparisons so that you - the programmer - can get exactly what you want in each circumstance.

==Numeric Equality (same value)also != < <= > and >=
eqString Equality (same characters)also ne lt le gt and ge
=~Pattern match (looks like a pattern)also !~


As from Perl 5.10, the ~~ intelligent match operator provides a single syntax through which the most appropriate of these will be chosen in each circumstance, bearing in mind the types of operands passed in. So:

use feature ':5.10';
 
$first = "Hello";
$second = "World";
 
say ("numerically the same") if ($first == $second);
say ("stringily the same") if ($first eq $second);
say ("look the same") if ($first =~ $second);
 
say ("intelligently the same") if ($first ~~ $second);


reports just:
  -bash-4.0$ perl im10
  numerically the same
  -bash-4.0$

as the natural way to compare two strings is the eq comparator.

But the ~~ (intelligent match) operator goes further - it provides all sort of other natural comparisons (there's an authoritative list [here]) that can save you the need for loops and other obfuscations of your code. For example - if you compare two lists, Perl's ~~ operator will (intelligently) compare each item in the first list with the item at the same position in the second list, and will return you a true value if and only if both lists have the same length, and each item is equal to its equivalent item:

use feature ':5.10';
 
@keywords = ("Melksham","Hotel","Community","Open Source","Training","Courses");
@morekeys = ("Melksham","Hotel","Community","Open Source","Courses","Training");
 
say ("The lists are identical (1)") if (@keywords ~~ @morekeys);
 
@kw1 = sort(@keywords);
@kw2 = sort(@morekeys);
 
say ("The lists are identical (2)") if (@kw1 ~~ @kw2);


And that reports
  -bash-4.0$ perl im2
  The lists are identical (2)
  -bash-4.0$

On the basis that the two initial lists weren't the same - the contained the same things in a different order. But once sorted, they become the same in every element.

As a final example, what happens when we compare a list to a hash?
  say ("They match") if (@keywords ~~ %wiltshire);

The hash is said to match the list if any of the keys of the hash is equal to any member of the list. So what we're saying (in a simple comparison) is "do you have data for any of [these] in the hash table?".

So this program:

use feature ':5.10';
 
@keywords = ("Melksham","Hotel","Community","Open Source","Training","Courses");
 
%wiltshire = (
  Salisbury => "Cathedral City in South of County",
  Stonehenge => "Ancient Tourist Attraction",
  Pewsey => "Small Town in a Vale");
 
say ("They match (1)") if (@keywords ~~ %wiltshire);
 
$wiltshire{"Melksham"} = "Friendly town in the west of the county";
 
say ("They match (2)") if (@keywords ~~ %wiltshire);


does not match the first time, but does the second.
  -bash-4.0$ perl im3
  They match (2)
  -bash-4.0$





Writing your own modules - and using them from the CPAN too.We cover matching on all of our fundamental Perl Courses - and we revise and revisit the subject during advanced courses too. The "Intelligent Matching" is currently [June 2010] covered and demonstrated, but does not form a core part of later examples due to the limited adoption of Perl 5.10 and 5.12 up to this point / the need for most author's code to work with older releases of Perl. See [here] for a discussion of the issues that we have considered in making this (current) decision. This is a changing situation - on private courses, we'll be guided by the customer's needs, and over coming months / years, coverage of intelligent matching, given, say, etc will ease into our courses.

Picture - delegates on a recent private Perl course which I gave in Germany





Complete source code for the examples in this article may be found on our site - you are welcome to cut and paste them to try them out, but please note that they're demonstrations only and we cannot take responsibility for the consequences of their use in your production code. We do retain copyright, and we'll get rather upset with you if you copy loads of our examples and start giving unauthorized courses yourself based on our materials.

[source] - Intelligent match on scalars
[source] - Comparing Complete Lists
[source] - Checking for keys in a hash


(written 2010-06-18, updated 2010-06-20)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P205 - Perl - Initial String Handling
  [3770] Sample answers to training course exercises - available on our web site - (2012-06-21)
  [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
  [3547] Using Perl to generate multiple reports from a HUGE file, efficiently - (2011-12-09)
  [3411] Single and double quotes strings in Perl - what is the difference? - (2011-08-30)
  [3005] Lots of ways of doing it in Perl - printing out answers - (2010-10-19)
  [2963] Removing the new line with chop or chomp in Perl - what is the difference? - (2010-09-21)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2798] Perl - skip the classics and use regular expressions - (2010-06-08)
  [1860] Seven new intermediate Perl examples - (2008-10-30)
  [1849] String matching in Perl with Regular Expressions - (2008-10-20)
  [1608] Underlining in Perl and Python - the x and * operator in use - (2008-04-12)
  [1195] Regular Express Primer - (2007-05-20)
  [987] Ruby v Perl - interpollating variables - (2006-12-15)
  [970] String duplication - x in Perl, * in Python and Ruby - (2006-12-07)
  [324] The backtick operator in Python and Perl - (2005-05-25)
  [254] x operator in Perl - (2005-03-22)
  [31] Here documents - (2004-08-28)

P256 - Perl 6 Look Ahead
  [3077] Perl 6 - significantly nearer, and Rakudo looks very good - (2010-12-02)
  [2967] Multiway branches in Perl - the given and when syntax - (2010-09-22)
  [2817] Setting a safety net or fallback value in Perl - (2010-06-19)
  [2815] switch and case, or given and when in Perl - (2010-06-17)
  [2559] Moving the product forward - ours, and MySQL, Perl, PHP and Python too - (2010-01-01)
  [1721] Perl 6 - When will we have a production release? - (2008-07-26)
  [1417] What software version do we teach? - (2007-10-31)
  [1215] An update on Perl - where is it going? - (2007-06-03)
  [995] Ruby's case - no break - (2006-12-17)
  [582] DWIM and AWWO - (2006-01-30)
  [550] 2006 - Making business a pleasure - (2006-01-01)
  [113] A Parallel for Perl 6 - (2004-11-09)
  [89] When will Perl 6 be available - (2004-10-15)


Back to
switch and case, or given and when in Perl
Previous and next
or
Horse's mouth home
Forward to
Setting a safety net or fallback value in Perl
Some other Articles
Netiquette for forum newcomers
Some more pictures ...
File open and read in Perl - modernisation
Intelligent Matching in Perl
Python - splitting and joining strings
Iterating over a Perl list and changing all items
What is Perl?
Igloos melt in the summer, but houses do not
4287 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 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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2816_Int ... -Perl.html • PAGE BUILT: Sun Mar 30 15:20:58 2014 • BUILD SYSTEM: WomanWithCat