periapt has asked for the wisdom of the Perl Monks concerning the following question:
# the routine is an anonymous sub return sub { my $rec = shift; my $rtncd = 0; # define primary key structure # w/ unchanging values set my @key = ($statickey1, $statickey2, 0, 0, 0); # zeros are places for dynamic keys # key1 is initialized to zero if key not needed, # $keylmt[0] = 0 and $keylmt[1] = 0 so that outer loop is # only executed once # dynamic key1 for($key[2]=$keylmt[0]; $key[2] <= $keylmt[1]; ++$key[2]){ # dynamic key2 for($key[3]=$keylmt[2]; $key[3] <= $keylmt[3]; ++$key[3]){ # dynamic key3 for($key[4]=$keylmt[4]; $key[4]<=$keylmt[5]; ++$key[4]){ $rtncd = ProcessRecord($updt_sth,@key,$datavalue) } } } } # end anonymous sub # The ProcessRecord() sub does a variety of things including # the insertion of new records and the update of existing ones # the SQL statements are prepared from a hash using DBI like this # third key not needed $sql{updt_sth} = "UPDATE $dbf01..$table ". 'SET nrevts = ? '. 'WHERE statickey1 = ? AND statickey2 = ? '. 'AND 0 = ? AND key2 = ? AND key3 = ?'; # third key needed $sql{updt_sth} = "UPDATE $dbf01..$table ". 'SET nrevts = ? '. 'WHERE statickey1 = ? AND statickey2 = ? '. 'AND key1 = ? AND key2 = ? AND key3 = ?'; # the insert stmt is a sticking point as key1 may not exist # in table $sql{ins_sth} = "INSERT INTO $dbf01..$table". '(statickey1,statickey2,key1,key2,key3,value)'. 'VALUES(?, ?, ?, ?, ?, ?)'; # how the db actions are called sub InsNewRec{ # query placeholder order: (key, value) my $sth = shift; eval{$sth->execute(@_)}; return $@; } # end InsNewRec(); sub UpdtStoTbl{ # query placeholder order: (value,key) my $sth = shift; eval{$sth->execute(@_)}; return $@; } # end UpdtStoTbl(); sub SaveAmendedRec{ # query placeholder order: (key,chgfrom,value,chgdte) my $sth = shift; eval{$sth->execute(@_)}; return $@; } # end SaveAmendedRec();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Specifying a loop with varying number of keys
by dragonchild (Archbishop) on Mar 22, 2005 at 14:27 UTC | |
by periapt (Hermit) on Mar 23, 2005 at 13:03 UTC |