#!/usr/bin/newlisp

;; regexmatch - benchmark

;; get phone data from stdin
(while (read-line) (push (append (current-line) "\n") phones))
(reverse phones)

;; patterns
(set 'pattern (append
    {(?:^|[^\d\(])}		; must be preceeded by non-digit
    {(\()?}				; match 1: possible initial left paren
    {(\d\d\d)}			; match 2: area code is 3 digits
    {(?(1)\))}			; if match1 then match right paren
    {[ ]}				; area code followed by one space
    {(\d\d\d)}			; match 3: prefix of 3 digits
    {[ -]}			      ; separator is either space or dash
    {(\d\d\d\d)}			; match 4: last 4 digits
    {\D}				; must be followed by a non-digit
))

;; get N
(set 'N (integer (last (main-args))))
(set 'cnt 0)

(dotimes (i N)
  (dolist (phone phones)
    (if (regex pattern phone)
        (if (= i 0)
          (begin
            (inc 'cnt)
            (println (string cnt ": (" $2 ") " $3 "-" $4)))))))

;; eof


syntax highlighting with newLISP and syntax.cgi