How do you convert a TCL list into a string that PHP can read?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.tcl

    How do you convert a TCL list into a string that PHP can read?

    I have a TCL proc that needs to convert what might be a list into a
    string to read

    consider this:

    [set string [PROPER_CASE {hello world}]]; # OUTPUTS Hello World which
    is fine for PHP

    [set string [PROPER_CASE {-hello world}]]; # OUTPUT {{-Hello}}
    World, which PHP will print literally as {{-Hello}} World, instead of
    -Hello World

    Is there an easy way for TCL to brute-force a list into a
    strictly-string-only format to prevent this potentially ugly display
    from being displayed by PHP/etc.?

    Thanx
    Phil

  • MH

    #2
    Re: How do you convert a TCL list into a string that PHP can read?

    In article <1145579146.201 140.152200@e56g 2000cwe.googleg roups.com>,
    comp.lang.tcl <phillip.s.powe ll@gmail.com> wrote:[color=blue]
    >I have a TCL proc that needs to convert what might be a list into a
    >string to read
    >
    >consider this:
    >
    >[set string [PROPER_CASE {hello world}]]; # OUTPUTS Hello World which
    >is fine for PHP
    >
    >[set string [PROPER_CASE {&#045;hello world}]]; # OUTPUT {{-Hello}}
    >World, which PHP will print literally as {{-Hello}} World, instead of
    >-Hello World
    >
    >Is there an easy way for TCL to brute-force a list into a
    >strictly-string-only format to prevent this potentially ugly display
    >from being displayed by PHP/etc.?[/color]

    Not sure what "PROPER_CAS E" does..

    However, I think you want to look at the "join" command (you may need to call
    it multiple times).

    % set s "{{-Hello}} World"
    {{-Hello}} World
    % join $s
    {-Hello} World
    % join [join $s]
    -Hello World

    MH

    Comment

    • Bryan Oakley

      #3
      Re: How do you convert a TCL list into a string that PHP can read?

      comp.lang.tcl wrote:[color=blue]
      > I have a TCL proc that needs to convert what might be a list into a
      > string to read
      >
      > consider this:
      >
      > [set string [PROPER_CASE {hello world}]]; # OUTPUTS Hello World which
      > is fine for PHP
      >
      > [set string [PROPER_CASE {&#045;hello world}]]; # OUTPUT {{-Hello}}
      > World, which PHP will print literally as {{-Hello}} World, instead of
      > -Hello World
      >
      > Is there an easy way for TCL to brute-force a list into a
      > strictly-string-only format to prevent this potentially ugly display
      > from being displayed by PHP/etc.?[/color]

      To convert a list to a string with a space between each list element:

      join $someList " "

      So maybe you want:

      [set string [join [PROPER_CASE ...] " "]


      --
      Bryan Oakley

      Comment

      • slebetman@yahoo.com

        #4
        Re: How do you convert a TCL list into a string that PHP can read?

        comp.lang.tcl wrote:[color=blue]
        > I have a TCL proc that needs to convert what might be a list into a
        > string to read
        >
        > consider this:
        >
        > [set string [PROPER_CASE {hello world}]]; # OUTPUTS Hello World which
        > is fine for PHP
        >
        > [set string [PROPER_CASE {&#045;hello world}]]; # OUTPUT {{-Hello}}
        > World, which PHP will print literally as {{-Hello}} World, instead of
        > -Hello World
        >
        > Is there an easy way for TCL to brute-force a list into a
        > strictly-string-only format to prevent this potentially ugly display
        > from being displayed by PHP/etc.?[/color]

        The other replies here are right you may want to use join to "flatten"
        your list. But I also think your [PROPER_CASE] proc is written wrongly
        in that you intend it to return a string but instead it returns a list,
        worse still a double nested list:

        {
        {
        {
        -Hello
        }
        }
        World
        }

        which requires you to do [join] twice to flatten it out. I'd strongly
        recommend that you look at the PROPER_CASE proc and correct the bug
        there if it is under your control.

        Of course, [join] only flattens a list by 1 level and since your list
        is double nested you need to call [join] twice. But if you're not sure
        how many levels deep your list is nested here's a small proc to
        recursively flatten an arbitrarily nested list:

        proc flatten {ls} {
        set ret ""
        foreach x $ls {
        if {$x == "\{" || $x == "\}"} {
        # handle braces:
        lappend ret $x
        } elseif {[llength $x] > 1} {
        # recursively flatten sublist:
        set ret [concat $ret [flatten $x]]
        } else {
        lappend ret $x
        }
        }
        return $ret
        }

        So you can use it like:

        set string [flatten [PROPER_CASE {&#045;hello world}]]

        Comment

        • comp.lang.tcl

          #5
          Re: How do you convert a TCL list into a string that PHP can read?


          slebetman@yahoo .com wrote:[color=blue]
          > comp.lang.tcl wrote:[color=green]
          > > I have a TCL proc that needs to convert what might be a list into a
          > > string to read
          > >
          > > consider this:
          > >
          > > [set string [PROPER_CASE {hello world}]]; # OUTPUTS Hello World which
          > > is fine for PHP
          > >
          > > [set string [PROPER_CASE {-hello world}]]; # OUTPUT {{-Hello}}
          > > World, which PHP will print literally as {{-Hello}} World, instead of
          > > -Hello World
          > >
          > > Is there an easy way for TCL to brute-force a list into a
          > > strictly-string-only format to prevent this potentially ugly display
          > > from being displayed by PHP/etc.?[/color]
          >
          > The other replies here are right you may want to use join to "flatten"
          > your list. But I also think your [PROPER_CASE] proc is written wrongly
          > in that you intend it to return a string but instead it returns a list,
          > worse still a double nested list:
          >
          > {
          > {
          > {
          > -Hello
          > }
          > }
          > World
          > }
          >
          > which requires you to do [join] twice to flatten it out. I'd strongly
          > recommend that you look at the PROPER_CASE proc and correct the bug
          > there if it is under your control.
          >
          > Of course, [join] only flattens a list by 1 level and since your list
          > is double nested you need to call [join] twice. But if you're not sure
          > how many levels deep your list is nested here's a small proc to
          > recursively flatten an arbitrarily nested list:
          >
          > proc flatten {ls} {
          > set ret ""
          > foreach x $ls {
          > if {$x == "\{" || $x == "\}"} {
          > # handle braces:
          > lappend ret $x
          > } elseif {[llength $x] > 1} {
          > # recursively flatten sublist:
          > set ret [concat $ret [flatten $x]]
          > } else {
          > lappend ret $x
          > }
          > }
          > return $ret
          > }
          >
          > So you can use it like:
          >
          > set string [flatten [PROPER_CASE {-hello world}]][/color]


          Thanx for the proc, however, tclsh locks up tight, bringing down PHP
          and Apache and all web services in the process, if you try this:

          set string [flatten [PROPER_CASE {{-hello} {world}}]]

          If you have a string with curly braces and a dash, it blows up. Take
          one or the other away, all is well.

          Phil

          Comment

          • Bryan Oakley

            #6
            Re: How do you convert a TCL list into a string that PHP can read?

            comp.lang.tcl wrote:[color=blue]
            > Thanx for the proc, however, tclsh locks up tight, bringing down PHP
            > and Apache and all web services in the process, if you try this:
            >
            > set string [flatten [PROPER_CASE {{-hello} {world}}]]
            >
            > If you have a string with curly braces and a dash, it blows up. Take
            > one or the other away, all is well.[/color]

            "it blows up" does nothing to help us figure out the problem. Do you get
            any sort of tcl error you can show us, either on the web page or in a
            log? It really sounds like your problem is that PROPER_CASE is buggy,
            and/or it is improperly documented.

            Would it be possible for you to show us the result of the following command?
            [list PROPER_CASE [info args PROPER_CASE] [info body PROPER_CASE]]




            --
            Bryan Oakley

            Comment

            • comp.lang.tcl

              #7
              Re: How do you convert a TCL list into a string that PHP can read?


              Bryan Oakley wrote:[color=blue]
              > comp.lang.tcl wrote:[color=green]
              > > Thanx for the proc, however, tclsh locks up tight, bringing down PHP
              > > and Apache and all web services in the process, if you try this:
              > >
              > > set string [flatten [PROPER_CASE {{-hello} {world}}]]
              > >
              > > If you have a string with curly braces and a dash, it blows up. Take
              > > one or the other away, all is well.[/color]
              >
              > "it blows up" does nothing to help us figure out the problem.[/color]

              Sorry, there is no further description I can give you. tclsh locks,
              produces 100% CPU usage when you view via top, no error logs of any
              kind.

              Here is PROPER_CASE:

              [TCL]
              ############### ############### ############### ############### ############### ############
              #

              # Proc PROPER_CASE - this proc will convert string text into "marquee"
              style
              # by displaying its "proper case" (or in this case: "Proper Case").
              The
              # first letter of each word is capitalized except for small words
              (yet,
              # small words are capitalized if they are the first word in the text).

              # Special capitalization consideration is taken for common ethnic
              words,
              # hyphenated words, and words adjoined by underscores. Also
              consideration is
              # taken for words surrounded by non-alphabetic characters (updated
              7/10/2000)
              #

              # UPDATE 11/21/00: Exemptions within the phrase are now accepted
              within
              # PROPER_CASE to allow for words within phrases to not be capitalized.

              #

              # SYNTAX: set myPhrase [PROPER_CASE $myPhrase] {for all words}

              # set myPhrase [PROPER_CASE -exempt[list of exempted words]
              -- $myPhrase]
              #
              # IMPORTANT: If you include a list of words to be exempted you MUST
              include the
              # following:
              #
              # -exempt : This flag indicates an exemption list is to
              follow
              # -- : End of exemption list
              #
              # Without including "-exempt" before your exemption list
              there will be no
              # words to exempt; without including "--" it cannot deduce
              the end of the
              # exemption list and this proc will return an empty string.
              #

              # Written by Phil Powell. All rights reserved. on 2/14/00, updated
              7/10/00, 11/21/00
              #
              ############### ############### ############### ############### ############### ###########
              proc PROPER_CASE {args} {
              set lb [format %c 91]; set rb [format %c 93]; set bslash [format %c
              92]
              lappend statesList AL AK AR AZ CA CO CT DE DC FL GA HI ID IL IN IA KS
              KY LA ME MD MA
              lappend statesList MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA
              RI SC SD TN TX
              lappend statesList UT VT VA WA WV WI WY PR GU VI
              lappend directionsList NE NW SE SW N.E. S.E. S.W. N.W.
              set exemptFlag {}; set exemptHash {}; set phrase {}
              if {[regexp -nocase -- "-exempt" [lindex $args 0]]} {
              set exemptFlag [lindex $args 0]
              set i 1
              if {[lsearch -exact $args "--"] < 0} {set exemptFlag {}}
              if {[string length $exemptFlag] > 0} {
              while {![regexp -- "--" [lindex $args $i]]} {
              lappend exemptList "[lindex $args $i]"
              incr i
              }
              set exemptHash [lindex $args $i]
              set phrase [lindex $args [expr {$i + 1}]]
              }
              } else {
              set phrase [lindex $args 0]
              }
              regsub -all {"} $phrase "%1%" phrase
              regsub -all "$bslash[set lb](.*)$bslash$rb" $phrase "%2%[set
              bslash]1%2%" phrase
              lappend smallWords a an in of
              lappend exemptionVars statesList directionsList
              if {[string length $exemptFlag] > 0 && [string length $exemptHash] > 0
              && [info exists exemptList]} {
              lappend exemptionVars exemptList
              }
              for {set i 0} {$i < [llength $phrase]} {incr i} {
              set word [lindex $phrase $i]
              set isExempted 0
              foreach smallWord $smallWords {
              if {$smallWord == $word && $i > 0} { set isExempted 1 }
              }
              if {$word == [string toupper $word] && !$isExempted} {set isExempted
              1}
              if {!$isExempted} {
              foreach exemptionVar $exemptionVars {
              if {[lsearch -exact [set $exemptionVar] "$word"] >=0} {set
              isExempted 1}
              }
              }

              if {!$isExempted} {

              foreach char "- _" {

              set foundChar 0
              if {[regexp -- "$char" $word]} {
              set word [split $word "$char"]
              set foundChar 1
              }

              for {set j 0} {$j < [llength $word]} {incr j} {
              set wordlet [lindex $word $j]
              set beginIndx 0; set nonWord {}
              while {![regexp -nocase {[a-z]} [string index $wordlet $beginIndx]]}
              {
              append nonWord [string index $wordlet $beginIndx]
              incr beginIndx
              }
              ### Check to see if word is "Scots/Irish" but > 2 chars
              set tinyWord 0; set letter {}
              if {[expr [string length $wordlet] - [string length $nonWord]] <
              3} {
              ### Avoid setting of string range > 1 if word < 2 chars
              set tinyWord 1
              set endIndx [expr [string length $wordlet] - 1]
              } else {
              set endIndx [expr [string length $nonWord] + 1]
              }
              set snippet [string tolower [string range $wordlet $beginIndx
              $endIndx]]
              if {!$tinyWord} {
              set letter [string index $wordlet [expr 2 + [string length
              $nonWord]]]
              if {($snippet == "mc" || $snippet == "o'")} {
              set letter [string toupper $letter]
              }
              }

              set tempsnippet "$nonWord[string toupper [string index $snippet
              0]]"
              if {$endIndx > 0} {
              append tempsnippet [string index $snippet 1]
              }
              set snippet $tempsnippet

              set tempwordlet $snippet$letter
              if {!$tinyWord} {
              append tempwordlet [string range $wordlet [expr 3 + [string
              length $nonWord]] end]
              }
              set wordlet $tempwordlet

              set word [lreplace $word $j $j $wordlet]
              }; # end of "j" for loop

              if {$foundChar} {
              set word [join $word "$char"]
              }
              }; # End of foreach

              set phrase [lreplace $phrase $i $i $word]
              }; # end of "if {!$isExempted}"

              }; # end of outer for loop
              regsub -all "%1%" $phrase {"} phrase
              regsub -all "%2%(.*)%2% " $phrase "$lb[set bslash]1$rb" phrase
              return $phrase
              }
              [/TCL]

              Do you get[color=blue]
              > any sort of tcl error you can show us, either on the web page or in a
              > log? It really sounds like your problem is that PROPER_CASE is buggy,
              > and/or it is improperly documented.
              >
              > Would it be possible for you to show us the result of the following command?
              >
              >[list PROPER_CASE [info args PROPER_CASE] [info body PROPER_CASE]]
              >
              >
              >
              >
              > --
              > Bryan Oakley
              > http://www.tclscripting.com[/color]

              Comment

              • Bryan Oakley

                #8
                Re: How do you convert a TCL list into a string that PHP can read?

                comp.lang.tcl wrote:[color=blue]
                > Sorry, there is no further description I can give you. tclsh locks,
                > produces 100% CPU usage when you view via top, no error logs of any
                > kind.
                >
                > Here is PROPER_CASE:
                > <snip>[/color]

                Holy cow! All that just to change the case of words in a string?

                The proc is a bit buggy at first glance. It's amazing it works at all.
                It takes a string, does some string operations on it, then iterates over
                it as if it were a list and performs list operations on it. Then, it
                takes the list, performs string operations on it and returns a string.

                Indeed, testing it out by copying it into a tclsh session, it _is_
                buggy. If the first char is "-" it gets in an infinite loop.

                Maybe you should file a bug with whomever wrote PROPER_CASE instead of
                trying to work around its limitations.

                --
                Bryan Oakley

                Comment

                • Gerald W. Lester

                  #9
                  Re: How do you convert a TCL list into a string that PHP can read?

                  Bryan Oakley wrote:[color=blue]
                  > comp.lang.tcl wrote:[color=green]
                  >> Sorry, there is no further description I can give you. tclsh locks,
                  >> produces 100% CPU usage when you view via top, no error logs of any
                  >> kind.
                  >>
                  >> Here is PROPER_CASE:
                  >> <snip>[/color]
                  >
                  > Holy cow! All that just to change the case of words in a string?
                  >
                  > The proc is a bit buggy at first glance. It's amazing it works at all.
                  > It takes a string, does some string operations on it, then iterates over
                  > it as if it were a list and performs list operations on it. Then, it
                  > takes the list, performs string operations on it and returns a string.
                  >
                  > Indeed, testing it out by copying it into a tclsh session, it _is_
                  > buggy. If the first char is "-" it gets in an infinite loop.
                  >
                  > Maybe you should file a bug with whomever wrote PROPER_CASE instead of
                  > trying to work around its limitations.
                  >[/color]

                  It does a *little* more than [string totitle $string] -- it ignores two
                  letter state "names" -- but not much.

                  My guess is this is a "translatio n" of a procedure from another language by
                  someone who does not have a good knowledge of Tcl.

                  --
                  +--------------------------------+---------------------------------------+
                  | Gerald W. Lester |
                  |"The man who fights for his ideals is the man who is alive." - Cervantes|
                  +------------------------------------------------------------------------+

                  Comment

                  • comp.lang.tcl

                    #10
                    Re: How do you convert a TCL list into a string that PHP can read?


                    Gerald W. Lester wrote:[color=blue]
                    > Bryan Oakley wrote:[color=green]
                    > > comp.lang.tcl wrote:[color=darkred]
                    > >> Sorry, there is no further description I can give you. tclsh locks,
                    > >> produces 100% CPU usage when you view via top, no error logs of any
                    > >> kind.
                    > >>
                    > >> Here is PROPER_CASE:
                    > >> <snip>[/color]
                    > >
                    > > Holy cow! All that just to change the case of words in a string?
                    > >
                    > > The proc is a bit buggy at first glance. It's amazing it works at all.
                    > > It takes a string, does some string operations on it, then iterates over
                    > > it as if it were a list and performs list operations on it. Then, it
                    > > takes the list, performs string operations on it and returns a string.
                    > >
                    > > Indeed, testing it out by copying it into a tclsh session, it _is_
                    > > buggy. If the first char is "-" it gets in an infinite loop.
                    > >
                    > > Maybe you should file a bug with whomever wrote PROPER_CASE instead of
                    > > trying to work around its limitations.
                    > >[/color]
                    >
                    > It does a *little* more than [string totitle $string] -- it ignores two
                    > letter state "names" -- but not much.
                    >[/color]

                    Last I checked [string totitle] doesn't capitalize hyphenated names,
                    Scots/Irish/Dutch names, etc. I wrote it back in 2000, when I only had
                    about a year's knowledge of TCL at the time, I'm sure it needs a bit of
                    fine-tuning, but it does the job it's supposed to do.

                    Phil
                    [color=blue]
                    > My guess is this is a "translatio n" of a procedure from another language by
                    > someone who does not have a good knowledge of Tcl.
                    >
                    > --
                    > +--------------------------------+---------------------------------------+
                    > | Gerald W. Lester |
                    > |"The man who fights for his ideals is the man who is alive." - Cervantes|
                    > +------------------------------------------------------------------------+[/color]

                    Comment

                    • Bryan Oakley

                      #11
                      Re: How do you convert a TCL list into a string that PHP can read?

                      Bryan Oakley wrote:
                      [color=blue]
                      > Maybe you should file a bug with whomever wrote PROPER_CASE instead of
                      > trying to work around its limitations.
                      >[/color]

                      FWIW, the culprit is this infinite loop:

                      while {![regexp -nocase {[a-z]} [string index $wordlet $beginIndx]]} {
                      append nonWord [string index $wordlet $beginIndx]
                      incr beginIndx
                      }

                      Under the right conditions (such as a word beginning or ending with "-"
                      or "_"), $wordlet will be null. When wordlet is null, [string index
                      $wordlet $beginIndx] will be null for all values of $beginIndx, the
                      regexp will never match, and the loop will never terminate.

                      --
                      Bryan Oakley

                      Comment

                      • Bryan Oakley

                        #12
                        Re: How do you convert a TCL list into a string that PHP can read?

                        comp.lang.tcl wrote:
                        [color=blue]
                        > Last I checked [string totitle] doesn't capitalize hyphenated names,
                        > Scots/Irish/Dutch names, etc. I wrote it back in 2000, when I only had
                        > about a year's knowledge of TCL at the time, I'm sure it needs a bit of
                        > fine-tuning, but it does the job it's supposed to do.
                        >
                        > Phil[/color]

                        It's pretty fragile though. As I pointed out in another message in this
                        thread, it can get into an infinite loop if a word begins or ends with
                        "-" or "_". It also will yield unexpected results for other types of
                        input as well. So, it really only works for a small set of well behaved
                        inputs.

                        We can help you with those problems if you like.

                        Not sure what to do about the OP, other than to suggest perhaps writing
                        their own PROPER_CASE proc or help to debug this one. There appears to
                        be no good way to solve his/her problem other than to fix PROPER_CASE.

                        --
                        Bryan Oakley

                        Comment

                        • comp.lang.tcl

                          #13
                          Re: How do you convert a TCL list into a string that PHP can read?


                          Bryan Oakley wrote:[color=blue]
                          > Bryan Oakley wrote:
                          >[color=green]
                          > > Maybe you should file a bug with whomever wrote PROPER_CASE instead of
                          > > trying to work around its limitations.
                          > >[/color]
                          >
                          > FWIW, the culprit is this infinite loop:
                          >
                          > while {![regexp -nocase {[a-z]} [string index $wordlet $beginIndx]]} {
                          > append nonWord [string index $wordlet $beginIndx]
                          > incr beginIndx
                          > }[/color]

                          WOW! I would never have found that one, you are truly one of the TCL
                          gurus out there (you came highly recommended by those I know)

                          this seemed to have fixed the problem (probably overkill but it was the
                          best I could think up at the moment:

                          while {[info exists wordlet] && [string length $wordlet] > 0 &&
                          ![regexp -nocase {[a-z]} [string index $wordlet $beginIndx]]} {
                          append nonWord [string index $wordlet $beginIndx]
                          incr beginIndx
                          }

                          Phil
                          [color=blue]
                          >
                          > Under the right conditions (such as a word beginning or ending with "-"
                          > or "_"), $wordlet will be null. When wordlet is null, [string index
                          > $wordlet $beginIndx] will be null for all values of $beginIndx, the
                          > regexp will never match, and the loop will never terminate.
                          >
                          > --
                          > Bryan Oakley
                          > http://www.tclscripting.com[/color]

                          Comment

                          • Bryan Oakley

                            #14
                            Re: How do you convert a TCL list into a string that PHP can read?

                            comp.lang.tcl wrote:[color=blue]
                            >
                            > WOW! I would never have found that one, you are truly one of the TCL
                            > gurus out there (you came highly recommended by those I know)[/color]

                            Thanks for the compliment but finding it took two minutes. I simply put
                            a print statement at the top of every loop, then tried a couple of very
                            obvious tests.
                            [color=blue]
                            > this seemed to have fixed the problem (probably overkill but it was the
                            > best I could think up at the moment:
                            >
                            > while {[info exists wordlet] && [string length $wordlet] > 0 &&
                            > ![regexp -nocase {[a-z]} [string index $wordlet $beginIndx]]} {
                            > append nonWord [string index $wordlet $beginIndx]
                            > incr beginIndx
                            > }[/color]

                            Yeah, that definitely helps that particular problem but it doesn't fix
                            all problems in the code.

                            Since you seem new to programming, you might want to look at tcltest.
                            Once you are up to speed on it (which doesn't take long), it would take
                            about 5-10 minutes to craft a couple dozen tests to validate the proc
                            against a whole range of inputs. That would have uncovered many bugs.




                            --
                            Bryan Oakley

                            Comment

                            • slebetman@yahoo.com

                              #15
                              Re: How do you convert a TCL list into a string that PHP can read?

                              comp.lang.tcl wrote:[color=blue]
                              > Gerald W. Lester wrote:[color=green]
                              > > Bryan Oakley wrote:[color=darkred]
                              > > > comp.lang.tcl wrote:
                              > > >> Sorry, there is no further description I can give you. tclsh locks,
                              > > >> produces 100% CPU usage when you view via top, no error logs of any
                              > > >> kind.
                              > > >>
                              > > >> Here is PROPER_CASE:
                              > > >> <snip>
                              > > >
                              > > > Holy cow! All that just to change the case of words in a string?
                              > > >
                              > > > The proc is a bit buggy at first glance. It's amazing it works at all.
                              > > > It takes a string, does some string operations on it, then iterates over
                              > > > it as if it were a list and performs list operations on it. Then, it
                              > > > takes the list, performs string operations on it and returns a string.
                              > > >
                              > > > Indeed, testing it out by copying it into a tclsh session, it _is_
                              > > > buggy. If the first char is "-" it gets in an infinite loop.
                              > > >
                              > > > Maybe you should file a bug with whomever wrote PROPER_CASE instead of
                              > > > trying to work around its limitations.
                              > > >[/color]
                              > >
                              > > It does a *little* more than [string totitle $string] -- it ignores two
                              > > letter state "names" -- but not much.
                              > >[/color]
                              >
                              > Last I checked [string totitle] doesn't capitalize hyphenated names,
                              > Scots/Irish/Dutch names, etc. I wrote it back in 2000, when I only had
                              > about a year's knowledge of TCL at the time, I'm sure it needs a bit of
                              > fine-tuning, but it does the job it's supposed to do.[/color]

                              You should factor out some of the code in there. Not because it is
                              faster, or to make the code re-usable or even to save memory but simply
                              to make it easier to understand. Here's a work-alike version I whipped
                              up in about 2 minutes:

                              ######
                              # Reformats a string to have proper case
                              ######
                              proc properCase {str} {
                              set ret ""
                              foreach x [split $str " \t\n\r"] {
                              append ret "[caseFormat [string trim $x]]"
                              append ret " "
                              }
                              return $ret
                              }

                              ########
                              # Applies case formatting rule to a word
                              ########
                              proc caseFormat {word} {
                              if {[set ret [irishScotFormat $word]] != ""} {
                              return $ret
                              }
                              if {[set ret [smallWordFormat $word]] != ""} {
                              return $ret
                              }
                              if {[set ret [statesFormat $word]] != ""} {
                              return $ret
                              }
                              return [string totitle $word [firstAlpha $word] end]
                              }

                              # Returns first alpha char in a word
                              proc firstAlpha {word} {
                              for {set i 0} {$i < [string length $word]} {incr i} {
                              if {[string is alpha [string index $word $i]]} {
                              return $i
                              }
                              }
                              return end
                              }

                              ############### # Formatting rules:

                              proc smallWordFormat {word} {
                              if {[lsearch -exact {
                              a an in of and at is
                              } $word] != -1} {
                              return $word
                              }
                              return ""
                              }

                              proc irishScotFormat {word} {
                              return ""
                              }

                              proc statesFormat {word} {
                              set word [string toupper $word]
                              if {[lsearch -exact {
                              AL AK AR AZ CA CO CT DE DC FL GA
                              HI ID IL IN IA KS KY LA ME MD MA
                              MI MN MS MO MT NE NV NH NJ NM NY
                              NC ND OH OK OR PA RI SC SD TN TX
                              UT VT VA WA WV WI WY PR GU VI NE
                              NW SE SW N.E. S.E. S.W. N.W.
                              } $word] != -1} {
                              return $word
                              }
                              return ""
                              }


                              I haven't implemented the irishScotFormat proc partly because I'm not
                              sure what you want and partly because I'm too lazy to do it. Notice
                              that the bulk of the processing happens in caseFormat which only have
                              to handle a single word at a time. This simplifies the properCase proc
                              to simply parse the string a word at a time without having to worry
                              about the processing. It's also very easy to add in more formatting
                              checks in the caseFormat proc.

                              Hope this helps you a little, I just had to re-implement it, the
                              [PROPER_CASE] proc was too ugly for me to resist.

                              Comment

                              Working...