a perl question

Randal Schwartz merlyn at iwarp.intel.com
Wed Nov 15 12:24:19 AEST 1989


In article <RJK.89Nov9162936 at sawmill.uucp>, rjk at sawmill (Richard Kuhns) writes:
| I'm not entirely sure that this is the newsgroup I should use, but
| I've seen a number of perl questions/answers and I don't know of a
| better newgroup (until comp.lang.perl comes along).
| 
| My question:  I'd dearly love to have a filter, written in perl (the
| rest of the code for this project is in perl, and I'll post it when I
| get it working), which would turn the string `B^HBO^HOL^HLD^HD' into
| `$bold_startBOLD$bold_end', where $bold_start and $bold_end are
| predefined character strings.  I have a filter that does this already
| written in C, but it seems to me I should be able to do it easier in
| perl (using regular expressions?), but I can't come up with a good way
| to do it.  /(.)\010$1/ recognizes one element of such a string (always
| the first).  s/(.)\010$1/$1/g specifically does NOT work (it only
| changes the first occurence).

I saw this question come through the perl-users at virginia.edu mailing
list first, but I'll post my reply here (being the token Perl
wizard...:-):

#!/usr/bin/perl
$bold_start = "whatever"; $bold_end = "whatever";
while (<>) {
	if (/\010/) {
		s/(.)\010\1/\201\1\202/g; # surround bold with \201 and \202
		s/\202\201//g; # optimize away all end-start pairs
		s/\201/$bold_start/og; # replace start with real start
		s/\202/$bold_end/og; # and likewise for end
	}
	print;
}

There you have it.  OK, so it's not a one-liner... big deal.

Just another Perl hacker,
(lwall says he's "Not just another Perl hacker"... :-)
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |
| merlyn at iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/



More information about the Comp.unix.questions mailing list