hello list
i have got a program which, if it meets certain strings (a tag) in a
content variable will do a lot of database calls.
my (main) question is wheter it is worthwhile to create a txt-file of
the relevant data on each line and then build a hash based on this file.
this hash would probably never have more than a few hundred elements. my
aiming is partly to unstress the database server but primarily to
speeden things up as it is concerning a heavily visited website.
i have enclosed a script containg 2 versions of a program that should do
both of these.
i am curently unable to test things in their proper enviroment but
perhaps someone have had a similar situation and can suggest a solution.
thnaks
allan
#!perl -w
keys(my %tagHash) = 10000; #preallocate size
my $content = "mixed content <TAG ID=1>\nmore stuff <TAG ID=2>\n\n";
#several of several more lines like these
my $hashResponse = hashVersion($content);
print $hashResponse; #hash version
my $dbResponse = dbVersion($content);
print $dbResponse; #database version
############hash vesion subs############
sub hashVersion {
my $output = $_[0];
open (FILE, "dbm.txt"); #database dump-file; lines like <TAG ID=1>:123,00
local $/; #slurp
my $tables = <FILE>;
close(FILE);
GetTagHash($tables);
if ($output =~ /<TAG[^>]+id=\d+>/i) { #immidiate look thru content for TAGS
while ((my $tag, my $tagValue) = each %tagHash) { #loop thru hash
elements
$output =~ s/$tag/$tagValue/ig; #replace
}
}
return $output;
}
sub GetTagHash {
($_, my $costumer) = @_;
while (/^(<TAG[^>]+id=\d+>):([^:]+$)/igm) {
$tagHash{$1} = $2; #example: key:<TAG id=1>; value=123,00
}
return %tagHash;
}
############database vesion subs############
sub dbVersion {
$_ = $_[0];
while (/(<TAG[^>]+id=(\d+)>)/ig) {
my $tag = $1;
my $id = $2;
my $value = getValue($id); #function that calls a database, this will
happen a lot of times
s/$tag/$value/ig; #replace
}
return $_;
}
sub getValue{
#purely illustrating example; this would be some sql essentially just
returning a value
my $input = $_[0];
my $out;
if ($input==1) {$out = "123.00"}
elsif ($input==2) {$out = "456.00"}
else {$out = "789.00"}
return $out
}