Entry 3098
Tcl script
Submitted by anonymous
on Jan. 27, 2010 at 7:43 a.m.
Language: Tcl. Code size: 3.6 KB.
namespace eval phoneBooke { set ::phonelist [] set ::is_phone {\A[[:digit:]]{2,3}-?[[:digit:]]{7}\Z} set ::is_name {[a-zA-Z]+} proc myAdd {} { flush stdout puts "insert first name:\n"; gets stdin fname puts "insert last name:\n"; gets stdin lname while {true} { puts "insert phone number:\n"; gets stdin number if {[regexp $::is_phone $number] == 1} { break } else { puts "Invalid phone number (xx or xxx)-?(xxxxxxx)\n" } } lappend ::phonelist [list $fname $lname $number] tracePhoneBook $::phonelist } proc myRem {} { if {[llength $::phonelist] == 0} { puts "The phone book is empty, nothing left to delete\n"; mainMenu } set ln [llength $::phonelist] puts [format "There is %s records in the phonebook\nWhat record number do you want to delete?\n" $ln]; gets stdin line_no if {[regexp {\A[[:digit:]]\Z} $line_no] == 1} { if {$line_no < 1 || $line_no > $ln} { puts "Worning: you enter a out of range number"; mainMenu } elseif {$line_no > 1 && $line_no < $ln} { set ::phonelist [concat [lrange $::phonelist 0 [expr {$line_no-1}]] [lrange $::phonelist [expr {$line_no+1}] end]] } elseif {$line_no == 1} { set ::phonelist [lrange $::phonelist 1 end] } elseif {$line_no == $ln} { set ::phonelist [lrange $::phonelist 0 end-1] } } else { puts "Worning: you enter illegal number"; mainMenu } tracePhoneBook $::phonelist } proc tracePyyhoneBook {lst} { set STR "" [string append STR "<table>"] [string append STR "<tr><th>first name</th><th>last name</th><th>phone number</th></tr>"] foreach line $lst { [string append STR "<tr><td>[string trim [lindex $line 0]]</td><td>[string trim [lindex $line 1]]</td><td>[string trim [lindex $line 2]]]</td></tr>"] } [string append STR "</table>"] } proc f_compare {a b} { set a0 [lindex $a 0] set b0 [lindex $b 0] if {$a0 < $b0} { return -1 } elseif {$a0 > $b0} { return 1 } return [string compare [lindex $a 1] [lindex $b 1]] } proc l_compare {a b} { set a0 [lindex $a 1] set b0 [lindex $b 1] if {$a0 < $b0} { return -1 } elseif {$a0 > $b0} { return 1 } return [string compare [lindex $a 1] [lindex $b 1]] } proc p_compare {a b} { set a0 [lindex $a 2] set b0 [lindex $b 2] if {$a0 < $b0} { return -1 } elseif {$a0 > $b0} { return 1 } return [string compare [lindex $a 1] [lindex $b 1]] } proc sByFname {} { tracePhoneBook [lsort -command f_compare $::phonelist] } proc sByLname {} { tracePhoneBook [lsort -command l_compare $::phonelist] } proc sByPhone {} { tracePhoneBook [lsort -command p_compare $::phonelist] } proc mainMenu {} { flush stdout puts "\n\ Main menue\n-----------------\n\ 1). add item\n\ 2). remove item\n\ 3). trace the phone book.\n\ 4). sort by first name.\n\ 5). sort by last name.\n\ 6). sort by phone number.\n\ 7). quit.\n\ 8). re-init.\n\ Insert your option\n\ " set option [gets stdin] switch $option { 1 { myAdd } 2 { myRem } 3 { tracePhoneBook $::phonelist } 4 { sByFname } 5 { sByLname } 6 { sByPhone } 7 { break } 8 { init } default { puts stdout "Invalid option\n" #mainMenu } } } proc init {} { puts "The phone book was itiated with 3 records\nInsert option '3' to watch details" set ::phonelist [] lappend ::phonelist [list "Shlomi" "Elbaz" "03-6888320"] lappend ::phonelist [list "Moran" "Yasur" "03-6888321"] lappend ::phonelist [list "Oren" "Mondshein" "03-6888322"] ::phoneBooke::mainMenu } } ::phoneBooke::init
This snippet took 0.02 seconds to highlight.
Back to the Entry List or Home.