Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numbers ONLY Please!

Status
Not open for further replies.

GRonken

Programmer
Oct 30, 2003
26
0
0
US
I have a string which should have only numbers (ie telephone number), I am currently stripping out and checking the length to determine if the number has an area code and returning it in the format needed. What I need to do is to look and if ANY are non numeric, send a null or 11111111111 or something =like that.

Sorry about all the TEMP variables, not written by me .. !!! (NOT that I would have done any better!!)

set temp [split [lindex $xlateInVals 0] "-"]
set temp2 [string length $temp]
set temp3 [lindex $temp 0]
set temp4 [lindex $temp 1]
set temp5 [lindex $temp 2]
set nulvar ""
if {$temp2 == 0} {
set temp6 "$nulvar"
set xlateOutVals $temp6
} else {
if {$temp2 >8} {
set temp6 "$temp4-$temp5"
set xlateOutVals $temp6
} else {
set temp6 "$temp3-$temp4"
set xlateOutVals $temp6
}}
 
Obfuscated generic tcl solution.

Code:
proc createVars {lst sep args} {
set xlst [split $lst $sep]
   
      for {set i 0} {$i < [llength $xlst]} {incr i} {
           if {$i > [llength $args]} {break}
           set [lindex $args $i] [lindex $xlst $i] 
           puts &quot;[lindex $args $i] = [set [lindex $args $i]]&quot;
      }
return [expr $i + 1]
}


Code:
proc subOut {str pat rep} {
  regsub -all $pat $str $rep str
  return $str
}

Altogether:
Code:
 proc createVarsAll {lst sep args} {
set xlst [split $lst $sep]
   
      for {set i 0} {$i < [llength $xlst]} {incr i} {
           if {$i > [llength $args]} {break}
           set [lindex $args $i] [lindex $xlst $i] 
           puts &quot; Before: [lindex $args $i] = [set [lindex $args $i]]&quot;
           set [lindex $args $i] [subOut [set [lindex $args $i]] &quot;\[^0-9\]&quot; &quot;&quot;]
          puts &quot;After: [lindex $args $i] = [set [lindex $args $i]]&quot;
          if {[string length [set [lindex $args $i]]]} {lappend retlist [set [lindex $args $i]]}
      }
return $retlist
}

Then run it:
createVarsAll &quot;wicked wine 63488346 3467638 twelve&quot; &quot; &quot; one two three four
OUTPUT:
Before: one = wicked
After: one =
Before: two = wine
After: two =
Before: three = 63488346
After: three = 63488346
Before: four = 3467638
After: four = 3467638
Before: = twelve
After: =
63488346 3467638



 
EEEKKKS ...

I have this

$Three is my input which is &quot;Cell&quot;

regexp {([a-zA-Z]*)+$} $Three Check3

can I ? Do I set i [regexp {([a-zA-Z]*)+$} $Three Check3]

to check if one of the items in the input is in the list? .. shouldn't return a 1 if it is ? So if my input is &quot;222&quot; than it should return a O? Am I looking at this toally incorrect? Help ......... !!!

than I can simply check $i and see if it is a 0 or a 1 and do something from their
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top