[Zope-Perl] Send me your Perl Methods
Gisle Aas
gisle@ActiveState.com
06 Sep 2000 22:33:38 +0200
Michel Pelletier <michel@digicool.com> writes:
> 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?
sub digram {
my $word = shift;
my @digrams;
push(@digrams, '$' . substr($word, 0, 1));
push(@digrams, substr($word, $_, 2)) for 0 .. length($word)-1;
$digrams[-1] .= '$';
return \@digrams;
}
I'm not so sure about this being perl's forte. There was no way to
use regular expressions here :-)
I would actually like to see some methods that interact more with the
Zope environment.
Regards,
Gisle