[boulder.pm] line/loop optimization?
Jim Baker
boulder-pm at jim-baker.com
Tue May 8 11:47:14 CDT 2001
This is simple: get yourself a Perl Cookbook. From Recipe 8.1.6:
while (<CONFIG>) {
chomp; # no newline
s/#.*//; # no comments
s/^\s+//; # no leading white
s/\s+$//; # no trailing white
next unless length; # anything left?
my ($var, $value) = split(/\s*=\s*/, $_, 2);
$User_Preferences{$var} = $value;
}
will set up a hash %User_Preferences that you can do variable interpolation
into your templates. Very handy.
- Jim
-----Original Message-----
From: owner-boulder-pm-list at pm.org
[mailto:owner-boulder-pm-list at pm.org]On Behalf Of Robert L. Harris
Sent: Tuesday, May 08, 2001 10:28 AM
To: Boulder Perl Mongers
Subject: [boulder.pm] line/loop optimization?
Ok, I'm currently reading in a "config file" that says something like,
OPTION1=value1
OPTION2=value2
OPTION3=value3
OPTION4=value4
OPTION5=value5
and then reading in a template file that will have OPTIONX embeded. The
values will likely change per run so they can't be hard coded. I'm
currently doing something like this:
@Keys=keys(%ConfigOptions);
while(<TemplateIn>) {
chomp;
$Line=$_;
foreach $i (@Keys) {
$Line =~ s/$i/$ConfigOptions{$i}/;
}
}
Is there a better way to do a line by line substitution instead of looping
through @Keys each line?
Robert
:wq!
---------------------------------------------------------------------------
Robert L. Harris | Micros~1 :
Senior System Engineer | For when quality, reliability
at RnD Consulting | and security just aren't
\_ that important!
DISCLAIMER:
These are MY OPINIONS ALONE. I speak for no-one else.
FYI:
perl -e 'print $i=pack(c5,(41*2),sqrt(7056),(unpack(c,H)-2),oct(115),10);'
More information about the Boulder-pm
mailing list