develooper Front page | | Postings from April 2002

Re: Please explain

Thread Previous | Thread Next
Stephen Turner
April 8, 2002 13:38
Re: Please explain
Message ID:
On Mon, 8 Apr 2002, Jean-Pierre wrote:

> Although I have entered tpr(0,2) as a veteran, although I am in fact a 
> veteran, I am also a perl's newbie. I play golf because it is fun, of course, 
> but also because it helps me to read perldoc more and more.
> So : I caught  the construction "sort/./g", used by 60% of the solutions, on 
> the web, querying for "perl anagram".
> This was fine and good for me, but I do not understand how it works, and I 
> am unable to find a place (web or doc or book) where I can find an 
> explanation : where is the dictionary? from where are growing these words?
> Could someone give me either an explanation or a link?

First, sort puts its arguments into list context.

In list context, as long as there is () in the pattern or the /g switch,
// does not just return 1 if the match succeeds, but the parts matched
(parentheses if there were any, otherwise all the bits matched if there was
the /g switch). Read man perlop or Chapter 5 of the Camel book for the
details of this.

Here /./g matches all the letters in $_ individually, so in list context
/./g returns a list of all the letters in $_. This is then sorted by the
sort, and two words are anagrams if and only if they agree about their
sorted letters.

Stephen Turner, Cambridge, UK
"This is Henman's 8th Wimbledon, and he's only lost 7 matches." BBC, 2/Jul/01

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About