ASPN ActiveState Programmer Network
  ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups | Web Services
SEARCH

Reference
ActiveTcl 8.5
ActiveTcl 8.5.4.0 User Guide
ActiveTcl 8.5.4.0 Documentation Index
Tutorial
Adding & Deleting members of a list
Adding new commands to Tcl - proc
Assigning values to variables
Associative Arrays
Building reusable libraries - packages and namespaces
Changing Working Directory - cd, pwd
Channel I/O: socket, fileevent, vwait
Child interpreters
Command line arguments and environment strings
Creating Commands - eval
Debugging & Errors - errorInfo errorCode catch error return
Evaluation & Substitutions 1: Grouping arguments with ""
Evaluation & Substitutions 2: Grouping arguments with {}
Evaluation & Substitutions 3: Grouping arguments with []
File Access 101
Information about Files - file, glob
Information about procs - info
Introduction
Invoking Subprocesses from Tcl - exec, open
Learning the existence of commands and variables ? - info
Leftovers - time, unset
Looping 101 - While loop
Looping 102 - For and incr
Modifying Strings - tolower, toupper, trim, format
Modularization - source
More channel I/O - fblocked & fconfigure
More command construction - format, list
More Debugging - trace
More Examples Of Regular Expressions
More list commands - lsearch, lsort, lrange
More On Arrays - Iterating and use in procedures
More Quoting Hell - Regular Expressions 102
Numeric Comparisons 101 - if
Regular Expressions 101
Results of a command - Math 101
Simple pattern matching - "globbing"
Simple Text Output
State of the interpreter - info
String comparisons - compare match first last wordend
String Subcommands - length index range
Substitution without evaluation - format, subst
Tcl Data Structures 101 - The list
Textual Comparison - switch
Time and Date - clock
Variable scope - global and upvar
Variations in proc arguments and return values

MyASPN >> Reference >> ActiveTcl 8.5 >> ActiveTcl 8.5.4.0 User Guide >> ActiveTcl 8.5.4.0 Documentation Index >> Tutorial
ActiveTcl 8.5 documentation

String comparisons - compare match first last wordend

There are 6 string subcommands that do pattern and string matching. These are relatively fast operations, certainly faster than regular expressions, albeit less powerful.

string compare string1 string2
Compares string1 to string2 and returns:
  • -1 ..... If string1 is less than string2
  • 0 ........ If string1 is equal to string2
  • 1 ........ If string1 is greater than string2
These comparisons are done alphabetically, not numerically - in other words "a" is less than "b", and "10" is less than "2".
string first string1 string2
Returns the index of the character in string1 that starts the first match to string2, or -1 if there is no match.
string last string1 string2
Returns the index of the character in string1 that starts the last match to string2, or -1 if there is no match.
string wordend string index
Returns the index of the character just after the last one in the word which contains the index'th character of string. A word is any contiguous set of letters, numbers or underscore characters, or a single other character.
string wordstart string index
Returns the index of the character just before the first one in the word which contains the index'th character of string. A word is any contiguous set of letters, numbers or underscore characters, or a single other character.
string match pattern string
Returns 1 if the pattern matches string. The pattern is a glob style pattern.

Example

set fullpath "/usr/home/clif/TCL_STUFF/TclTutor/Lsn.17"
set relativepath "CVS/Entries"
set directorypath "/usr/bin/"

set paths [list $fullpath $relativepath $directorypath]

foreach path $paths  {
    set first [string first "/" $path]
    set last [string last "/" $path]

    # Report whether path is absolute or relative

    if {$first != 0} {
        puts "$path is a relative path"
    } else {
        puts "$path is an absolute path"
    }

    # If "/" is not the last character in $path, report the last word.
    # else, remove the last "/", and find the next to last "/", and
    #   report the last word.

    incr last
    if {$last != [string length $path]} {
        set name [string range $path $last end]
        puts "The file referenced in $path is $name"
    } else {
        incr last -2;
        set tmp [string range $path 0 $last]
        set last [string last "/" $tmp]
        incr last;
        set name [string range $tmp $last end]
        puts "The final directory in $path is $name"
    }

    # CVS is a directory created by the CVS source code control system.
    #

    if {[string match "*CVS*" $path]} {
        puts "$path is part of the source code control tree"
    }

    # Compare to "a" to determine whether the first char is upper or lower case
    set comparison [string  compare $name "a"]
    if {$comparison >= 0} {
        puts "$name starts with a lowercase letter\n"
    } else {
        puts "$name starts with an uppercase letter\n"
    }
}
   

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState 2004 All rights reserved