INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

check is a variable value is alphabet or numeric

check is a variable value is alphabet or numeric

check is a variable value is alphabet or numeric

(OP)
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

RE: check is a variable value is alphabet or numeric

Hi
  • Change these brackets ( [] ) to braces ( {} ) :

    Quote (ramana35):

    if [ [regexp {^([0-9]+)$} $variable] || ![string match {^([a-z]+)$} $var] ] {
  • Double check if you indeed intend to check once $variable then $var.
  • See if string is accomplishes you goal easier. ( Maybe you should describe your goal, I not really understand it. To me seems that you only need if {[string is alnum $var]} { #.... )
  • Next time please post your code between [code] and [/code] TGML tags.

Feherke.
http://feherke.github.com/

RE: check is a variable value is alphabet or numeric

(OP)
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 ?

 

RE: check is a variable value is alphabet or numeric

Hi

I would suggest to try the various character classes to see what they match :

CODE --> Tcl

set varlist {1234 1,234 -1234 ramana ramana35 "ramana 35"}
set typelist {alpha digit alnum integer}

puts -nonewline [format "%-10s" "value"]
foreach type $typelist {
  puts -nonewline [format " | is %-7s" $type]
}
puts ""

foreach var $varlist {
  puts -nonewline [format "%-10s" $var]
  foreach type $typelist {
    puts -nonewline [format " | %-10s" [expr [string is $type $var]?"TRUE":"-"]]
  }
  puts ""
}

CODE --> output

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.
http://feherke.github.com/

RE: check is a variable value is alphabet or numeric

(OP)
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.

 

RE: check is a variable value is alphabet or numeric

Hi

Note that capturing is pointless there, they are just slowing down the matching :

CODE --> Tcl ( fragment )

if { [ regexp {^[0-9]+$} $var ] || [ regexp {^[a-zA-Z]+$} $var ] } {
Unless you use the grouping for alternation, but even so you can make it non-capturing :

CODE --> Tcl ( fragment )

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

Feherke.
http://feherke.github.com/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close