Re: String manipulation, find/replace
From: Mark G. Saye (markgsaye_at_yahoo.com)
Date: 12/10/03
- Next message: TingChong: "Re: determine parent process id"
- Previous message: TingChong: "Re: determine parent process id"
- In reply to: David: "String manipulation, find/replace"
- Next in thread: Kaitzschu: "Re: String manipulation, find/replace"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: TingChong: "Re: determine parent process id"
- Previous message: TingChong: "Re: determine parent process id"
- In reply to: David: "String manipulation, find/replace"
- Next in thread: Kaitzschu: "Re: String manipulation, find/replace"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|