Re: Is it possible to have special characters in list
- From: "slebetman@xxxxxxxxx" <slebetman@xxxxxxxxx>
- Date: Sat, 01 Sep 2007 04:21:38 -0700
On Sep 1, 1:49 pm, Terminator <manidevara...@xxxxxxxxx> wrote:
Folks,
I am trying to loop through the following o/p which has special
characters in it like { & [. But when i run, it returns "unmatched
braces". Can you suggest how to handle this case.
Data:
0xf3a1a44c: 0x7BE6CAE2 0xBEA05B1A 0xD5D37E2B 0xA3AD6835 {.....[...~
+..h5
0xf3a1a45c: 0x81BA9D75 0xAAEFC8F5 0x72DCDAA4
0x92C3441E ...u....r.....D.
My code:
foreach elem $Data {
.....
}
It appears that $Data isn't a proper list. What is the delimiting
character in your $Data? If all the elements are delimited by a single
space then you can do:
foreach elem [split $Data " "] {
do_something $elem
}
If, as I suspect, $Data is whitespace delimited then you have to coax
it into a form that is more easily splitable:
regsub -all -- {\s+} $Data " " Data
foreach elem [split $Data " "] {
do_something $elem
}
In tcl, a list is not simply a string of whitespace separated
words(1). A list is a string of whitespace separated words(1) where
braces are properly quoted or escaped. That is why you should not
trust external data to be proper lists(2). You should always parse the
data first.
Notes:
(1) "Word" here is as recognised by the tcl command parser. For
example {this is one word} is a single word since it's grouped by {}.
(2) I actually often treat external data directly as lists. In which
case I define the expected data format to be a proper tcl list and so
any errors encountered is not considered a "bug" in my program but
user/configuration error ;-)
.
- References:
- Is it possible to have special characters in list
- From: Terminator
- Is it possible to have special characters in list
- Prev by Date: Re: getting user names with twapi
- Next by Date: Re: wicked pisser intro to tcl, wish I found earlier
- Previous by thread: Is it possible to have special characters in list
- Next by thread: Re: Is it possible to have special characters in list
- Index(es):
Relevant Pages
|
|