Subject: Re: Text based stage play scripts to XML
From: Jacobus Reyneke <jacobusreyneke@xxxxxxxxx>
Date: Mon, 24 Jan 2011 21:44:23 +0200
|
> #! /usr/bin/perl -w
> use warnings;
> use strict;
>
> sub element($$;%)
> {
> my ($name, $content, %attributes) = @_;
>
> sub quotedattvalue($$)
> {
> my ($name, $value) = @_;
>
> # print STDERR "q $name, $value\n";
> $value =~ s/"/\"/g; # so we can safely use quotes
> return '"' . $value . '"';
> }
>
> # make a list of att="value" pairs, each with a leading space:
> # (could use join and map to do this too more succinctly,
> # see perldoc -f map)
> my $atts = "";
> if (%attributes) {
> foreach (keys %attributes) {
> $atts .= " " .
> $_ . '=' . quotedattvalue($_, $attributes{$_})
> ;
> }
> }
>
> return "<${name}${atts}>${content}</${name}>";
> }
>
> my $text;
> {
> local $/;
> $text = <>;
> };
>
> $text =~ s{c d\K e f (?=g h)}{
> element(
> "found-you",
> $&,
> "rule" => "31",
> "before" => "c d"
> )
> }gme;
> print $text;
>
> # end
Liam, you are a genius. The effort and passion you put into your
response is commendable! Thanks a stack. I'm working it through. Since
the nlptk (natural language processing toolkit) is written in Python,
I assumed for some reason that Python was better suited for text
processing in a modern world - but Perl seems quite readable, logical
and practical.
Thanks again for the effort! And friendly "hello world" in Perl.
Kind regards,
Jacobus
|