sub update_database { my($page_url)=@_; my(@categorys); my($category); my($dbfield); @categorys = keys(%hash); foreach $category (@categorys) { # Key is prefixed to match database $dbfield="Prefix".$category; # Establish database connection with MySQL my $dsn = 'DBI:mysql:*database*:localhost'; my $db_user_name = '*username*'; my $db_password = '*password*'; my $dbh = DBI->connect($dsn, $db_user_name, $db_password); my $sth = $dbh->prepare(qq{ update table set $dbfield = '$hash{$category}' where pageurl = '$page_url' }); $sth->execute(); $sth->finish(); } }