Gary Stainburn <[EMAIL PROTECTED]> wrote:
:
: I liked you program so I took a copy. Then I thought
: I'd use it as an excercise in 'PERL'ifying it. By this
: I mean appling some of the things I've read about
: writing perl scripts, such as removing transient
: variables where possible, elliminaing variables who's
: sole purpose is to control the program, and above all
: take less typing.
:
: The only variable I can't seem to get rid of is the
: $input_line, although I'm sure someone will tell me
: how.
:
: #!/usr/bin/perl -w
:
: # Copyright 2003 by Michael Weber
: # Released into the public domain July 2003
:
: # 'PERLified' by Gary Stainburn July 2003
:
: use strict;
:
: die "$0 requires a color file. See readme.txt for more
: info.\n" if $#ARGV <
: 0;
:
: my %colours=('red'=>"\033[0;31m",
: 'brown'=>"\033[0;33m",
: 'blue'=>"\033[0;34m",
: 'green'=>"\033[0;32m",
: 'cyan'=>"\033[0;36m",
: 'purple'=>"\033[0;35m",
: 'gray'=>"\033[0;37m",
:
: 'ltred'=>"\033[1;31m",
: 'yellow'=>"\033[1;33m",
: 'ltblue'=>"\033[1;34m",
: 'ltgreen'=>"\033[1;32m",
: 'ltcyan'=>"\033[1;36m",
: 'ltpurple'=>"\033[1;35m",
: 'white'=>"\033[1;37m",
: 'ltgray'=>"\033[0;37m");
Are gray and ltgray supposed to be the same
value? I found this by using some whitespace:
my %colours = (
red => '\033[0;31m', ltred => '\033[1;31m',
green => '\033[0;32m', ltgreen => '\033[1;32m',
brown => '\033[0;33m', yellow => '\033[1;33m',
blue => '\033[0;34m', ltblue => '\033[1;34m',
purple => '\033[0;35m', ltpurple => '\033[1;35m',
cyan => '\033[0;36m', ltcyan => '\033[1;36m',
gray => '\033[0;37m', white => '\033[1;37m',
ltgray => '\033[0;37m',
);
: my $normal="\033[0m";
:
: my %triggers;
:
: open (CONF, $ARGV[0]) || die "Can't open config file $ARGV[0], $!\n";
:
: while (<CONF>) {
: chomp;
: my ($trigger,$color)=split(",");
: $triggers{$trigger}=($colours{$color}) ? $colours{$color} :
: $colours{"white"};
: }
:
: close (CONF);
:
: while (my $input_line = <STDIN>) {
:
: foreach (keys %triggers) {
: $input_line =~ s/($_)/$triggers{$_}$1${normal}/g;
: }
: print $input_line;
:
: }
Instead of the foreach loop, we could search
for multiple triggers in the substitution. And
we may as well do it case-insensitively also.
my $regex = join '|', keys %triggers;
while ( <STDIN> ) {
s/($regex)/$triggers{$1}$1${normal}/iog;
print;
}
HTH,
Charles K. Clarkson
--
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]