#!/usr/local/bin/tclsh
# $Id: regexmatch.tcl,v 1.9 2001/03/15 17:01:52 doug Exp $
# http://www.bagley.org/~doug/shootout/
# from: Miguel Sofer, with modifications by Kristoffer Lawson

proc main {} {
    global argv
    set NUM [lindex $argv 0]
    if {$NUM < 1} {
        set NUM 1
    }

    set phones [split [read stdin] "\n"]
    set count 0    
    set rExp {(?:^|[^\d(])(\(\d{3}\)|\d{3}) (\d{3})[ -](\d{4})($|[^\d])}

    while {$NUM > 0} {
    incr NUM -1
        foreach phone $phones {
            if {[regexp $rExp $phone match area exch num]} {
                if {! $NUM} {
                    incr count 1
            puts "$count: ([string trim $area () ]) $exch-$num"
                }
            }
        }
    }
}

main