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!

check is a variable value is alphabet or numeric

Status
Not open for further replies.

ramana35

Technical User
Apr 23, 2012
14
HK
Hi,

I am new to tcl,
Please help with below simple requirement

I will have a value in my variable, need to check wheather it is numeric or string.

i tried in below way, but did not work
if [ [regexp {^([0-9]+)$} $variable] || ![string match {^([a-z]+)$} $var] ] {
puts "either numeric or string"
}
else {
puts "not number or strin"

}


Please help
 
Hi
[ul]
[li]Change these brackets ( [] ) to braces ( {} ) :
ramana35 said:
if [red][[/red] [regexp {^([0-9]+)$} $variable] || ![string match {^([a-z]+)$} $var] [red]][/red] {
[/li]
[li]Double check if you indeed intend to check once $variable then $var.[/li]
[li]See if [tt]string is[/tt] accomplishes you goal easier. ( Maybe you should describe your goal, I not really understand it. To me seems that you only need [tt]if [teal]{[/teal][teal][[/teal]string is alnum [navy]$var[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal] [gray]#...[/gray][/tt]. )[/li]
[li]Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.[/li]
[/ul]

Feherke.
 
I have a variable which has value like 1234 or ramana

if it is either 1234 or ramana , i want to return true, else false

can you ell me how can i use string is ?

 
Hi

I would suggest to try the various character classes to see what they match :
Code:
[b]set[/b] varlist [teal]{[/teal][purple]1234[/purple] [purple]1[/purple][teal],[/teal][purple]234[/purple] [teal]-[/teal][purple]1234[/purple] ramana ramana35 [green][i]"ramana 35"[/i][/green][teal]}[/teal]
[b]set[/b] typelist [teal]{[/teal]alpha digit alnum integer[teal]}[/teal]

[b]puts[/b] [teal]-[/teal]nonewline [teal][[/teal][b]format[/b] [green][i]"%-10s"[/i][/green] [green][i]"value"[/i][/green][teal]][/teal]
[b]foreach[/b] type [navy]$typelist[/navy] [teal]{[/teal]
  [b]puts[/b] [teal]-[/teal]nonewline [teal][[/teal][b]format[/b] [green][i]" | is %-7s"[/i][/green] [navy]$type[/navy][teal]][/teal]
[teal]}[/teal]
[b]puts[/b] [green][i]""[/i][/green]

[b]foreach[/b] var [navy]$varlist[/navy] [teal]{[/teal]
  [b]puts[/b] [teal]-[/teal]nonewline [teal][[/teal][b]format[/b] [green][i]"%-10s"[/i][/green] [navy]$var[/navy][teal]][/teal]
  [b]foreach[/b] type [navy]$typelist[/navy] [teal]{[/teal]
    [b]puts[/b] [teal]-[/teal]nonewline [teal][[/teal][b]format[/b] [green][i]" | %-10s"[/i][/green] [teal][[/teal][b]expr[/b] [teal][[/teal][b]string[/b] is [navy]$type[/navy] [navy]$var[/navy][teal]]?[/teal][green][i]"TRUE"[/i][/green][teal]:[/teal][green][i]"-"[/i][/green][teal]]][/teal]
  [teal]}[/teal]
  [b]puts[/b] [green][i]""[/i][/green]
[teal]}[/teal]
Code:
value      | is alpha   | is digit   | is alnum   | is integer
1234       | -          | TRUE       | TRUE       | TRUE      
1,234      | -          | -          | -          | -         
-1234      | -          | -          | -          | TRUE      
ramana     | TRUE       | -          | TRUE       | -         
ramana35   | -          | -          | TRUE       | -         
ramana 35  | -          | -          | -          | -
Then you should specify more about what is acceptable and what is not. In meantime keep sustaining my suggestion made in point 3. on 23 Apr 12 8:58.

Feherke.
 
thanks ,
below one worked for me.

if { [ regexp {^([0-9]+)$} $var ] || [ regexp {^([a-zA-Z]+)$} $var ] } {

puts "true"
} else {
puts "false"

}

Anyways thanks for your replies.

 
Hi

Note that capturing is pointless there, they are just slowing down the matching :
Code:
[b]if[/b] [teal]{[/teal] [teal][[/teal] [b]regexp[/b] [teal]{[/teal][teal]^[[/teal][purple]0[/purple][teal]-[/teal][purple]9[/purple][teal]]+[/teal]$[teal]}[/teal] [navy]$var[/navy] [teal]][/teal] [teal]||[/teal] [teal][[/teal] [b]regexp[/b] [teal]{[/teal][teal]^[[/teal]a[teal]-[/teal]zA[teal]-[/teal]Z[teal]]+[/teal]$[teal]}[/teal] [navy]$var[/navy] [teal]][/teal] [teal]}[/teal] [teal]{[/teal]
Unless you use the grouping for alternation, but even so you can make it non-capturing :
Code:
[b]if[/b] [teal]{[/teal] [teal][[/teal] [b]regexp[/b] [teal]{[/teal][teal]^(?:[[/teal][purple]0[/purple][teal]-[/teal][purple]9[/purple][teal]]+|[[/teal]a[teal]-[/teal]zA[teal]-[/teal]Z[teal]]+)[/teal]$[teal]}[/teal] [navy]$var[/navy] [teal]][/teal] [teal]}[/teal] [teal]{[/teal]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top