May 2010 Archives

Mapping substitutions

I am always wondering what is the best procedure to perform a substitution on a set of elements. While map could be a good idea,
@foo = map { s/bar/foo/g; $_ } @bar
is not legible. Probably better to use
@foo = @bar; s/bar/foo/g for @foo;
While I understand that the substitute operator return value is useful and relevant, I wonder if there is a cleaner syntax for this behavior with map.

Word counting

There is a Perl module (by coincidence, also Portuguese authored) named Text::ExtractWords that performs more or less the same as the unix command toilet^H^H^H^H^H^Hwc. It returns an hash of words mapping them to their occurrence count.

The module is not bad. It is written in C making it quite fast when compared with Perl code on big strings. Unfortunately, it has a main limitation: unicode. Although it supports a 'locale' configuration parameter, it seems not to affect its behavior regarding unicode characters, that is, looking to them as single ASCII characters.

I do not have…

About Alberto Simões

user-pic I blog about Perl. D'uh!