[Zope-Perl] Send me your Perl Methods

Michel Pelletier michel@digicool.com
Wed, 6 Sep 2000 15:38:23 -0400 (EDT)


Gisle Aas wrote:
> 
> 
> I'm also looking for interesting examples :-)
>
> Do you have any interesting PythonMethod examples?  I can probably
> translate it to perl for you.

Yes, I thought that was a good approach.  I don't really have any good
python methods either, but I can come up with some.

Here's an algorithm I did in the Globbing Lexicon component of Zope. 
This turns a string into 'digrams' Which is a common wild-card searching
datastructure.  So:

  bob => ['$b', 'bo', 'ob', 'b$']
  python => ['$p', 'py', 'yt', 'th', 'ho', 'on', 'n$']

Dollar signs mark the beginning and ending of the word.

def digram(word):
    """  """
    digrams = []
    digrams.append('$' + word[0]) # mark the beginning

    for i in range(len(word)):
        digrams.append(word[i:i+2])

    digrams[-1] = digrams[-1] + '$' # mark the end

    return digrams

There's probably a better way to do it in Python, and this should be
Perl's forte.  Can someone come up with a reasonably cool Perl method
that did this?  You don't have to do it like I did it, do it any of the
myriad perl possiblities.

-Michel