Posted to tcl by patthoyts at Wed Jan 09 14:06:37 GMT 2008view pretty

A fragment that uses htmparse to extract the list of countries from some web page
State is an application array name.

Its called using something like
            if {[catch {htmlparse::parse \
                            -cmd [list ParseCountries ::State] $data} err]} {
                tk_messageBox -icon error -message $err -detail $::errorInfo
            }


proc ParseCountries {State tag slash param text} {
    upvar #0 $State state
    variable CountryCodes
    set tag [string tolower $tag]
    if {$tag eq "hmstart"} { set state(c_state) 0}
    if {$tag eq "select"} {
        if {$slash eq "/"} {
            set state(c_state) 0
        } elseif {[string match "*PartyCountry*" $param]} {
            set state(c_state) 1
        }
    }
    if {$state(c_state) && $tag eq "option" && $slash eq {}} {
        foreach p $param {
            foreach {k v} [split $p =] break
            if {$k eq "value"} {
                set name [string toupper [string range $text 0 end-3]]
                set CountryCodes($name) [string trim $v "\""]
            }
        }
    }
}