Re: String manipulation, find/replace

From: Mark G. Saye (markgsaye_at_yahoo.com)
Date: 12/10/03


Date: Wed, 10 Dec 2003 10:56:02 -0800
To: David <junk1@davidbevan.co.uk>

David wrote:
> I need to convert a string from this format...
>
> (A B (C D E (F G) H) I J)
>
> ...to this format...
>
> A B
> C D E
> F G
> H
> I J
>
> ...so i basicall need to do something like search the string from left
> to right and every time i get a '(' add one to counter n then replace
> the '(' with CRLF and n spaces, then for every ')' subtract one from n
> and then replace the ')' with CRLF and n spaces.
>
> My tcl is not up to it, but if someone can tell me how to do
> find/replace and how to deal with CRLF then that would be helpful!
>
> ...or if someone wants to write the whole script then that would be
> great!

OK, I'll bite. Here's a version of something that appears to work as
requested, but I'm sure that many people can improve upon it. I bet
there's some advanced regular expression that can work wonders (if you
can understand it, which I don't)

Note that <CR> is \r and <LF> is \n so <CRLF> is \r\n.

# ---
   set string "(A B (C D E (F G) H) I J)"

   set n 0
   set output ""
   for {set i 0} {$i < [string length $string]} {incr i} {
     set c [string index $string $i]
     switch -- $c {
       \( {
            incr n 1
            append output [format "\r\n%${n}s" " "]
          }
       \) {
            incr n -1
            append output [format "\r\n%${n}s" " "]
          }
       default {
            append output $c
       }
     }
   }
   puts "string = \[$string\]"
   puts "output = \[$output\]"
# ---

which produces:

string = [(A B (C D E (F G) H) I J)]
output = [
  A B
   C D E
    F G
    H
   I J
  ]

Hope this gets you started on the right track ...

Mark /

-- 
Mark G. Saye
markgsaye @ yahoo.com


Relevant Pages

  • String manipulation, find/replace
    ... I need to convert a string from this format... ... and then replace the ')' with CRLF and n spaces. ... David Bevan ...
    (comp.lang.tcl)
  • Re: String manipulation, find/replace
    ... David wrote: ... > I need to convert a string from this format... ... > find/replace and how to deal with CRLF then that would be helpful! ...
    (comp.lang.tcl)
  • Re: GNAT.Sockets Segfault in a task
    ... The format of the bounds part is not ... incoming data didn't contain any bounds and String'Input was interpreting ... If these are strings, are they from RFC 822 style data where string ... termination is by CRLF? ...
    (comp.lang.ada)
  • printing bits ... the right way
    ... \param str The array of characters where the resulting string is to ... \param format A custom format string specific to converting ... render an integer type into a binary string. ...
    (comp.lang.c)
  • Re: Date format detection
    ... Specifies the locale for which the date string is to be formatted. ... date format for this locale. ... the system default-date format for the specified locale. ... be enclosed within single quotation marks in the date format picture. ...
    (borland.public.delphi.thirdpartytools.general)