#!/usr/bin/perl
use strict;
use Text::Soundex;

sub soundex {
        my($str)=@_;
        my(@str)=split(/\W/,$str);
        for(my $i=0;$i<@str;++$i){
                $str[$i]=Text::Soundex::soundex($str[$i]);
        }
        return join(' ',@str);
}

sub quote {
	return map {s,','',igs; qq{'$_'}} @_;
}


open(FILE,"<damnificados.php");
local $/ = undef;
my $str = <FILE>;
close(FILE);
my @rows=($str=~m,(<tr .*?</tr.*?>),igs);
foreach my $row (@rows) {
	$row=~s,[\n\r],,igs;
	my @cells = ($row=~m,<td .*?>(.*?)</td,igs);
	@cells = map {
		s,<.*?>,,igs;
		s,&nbsp;, ,igs;
		s,\s+, ,igs;
		s, *$,,igs;
		s,^ *,,igs;
		tr,瘟逑瀅蘇羊斫琥跼傔般燮菶飯,aeiounAEIOUNaeiouAEIOUuUuU,;
		uc($_);
	} @cells;
	my $age = $cells[2];
	my $gender = $cells[3];
	if(($age=~m,[0-9],)||($gender=~m,^(M|F)$,i)) {
		push @cells, soundex($cells[1]);
		for(qw(2 3 4)) {
			if($cells[$_] eq '') {
				$cells[$_] = '?';
			}
		}
		shift @cells;
		print qq{insert into damnificados_stan values (}.join(',',quote(@cells)).qq{);\n};
	}
}
