#!/usr/bin/tclsh
puts "issue: brackets inside of array names calls\n"
set oFH [open ouch w]
puts $oFH "valid,1"
puts $oFH "0,2"
puts $oFH "\[helpme\],3"
close $oFH
set iFH [open ouch]
while { [gets $iFH line] > 0 } {
foreach { key value } [split $line ,] { break }
set O($key) $value
lappend keyList $key
}
close $iFH
puts "all keys:\n\t[join $keyList \n\t]\n"
puts "no filter:\n\t[join [array names O] \n\t]\n"
puts "easy filter *helpme*:"
puts "\t[join [array names O *helpme*] \n\t]\n"
puts "exact filter -exact \\\[helpme\\\]:"
puts "\t[join [array names O -exact \[helpme\]] \n\t]\n"
puts "argh filter -glob \\\[helpme\\\]:"
puts "\t[join [array names O -glob \[helpme\]] \n\t]\n"
puts "protected filter {\[helpme\]}:"
puts "\t[join [array names O -glob {[helpme]}] \n\t]\n"
puts "gak filter \[string map {\[ ? \] ?} \[helpme\]\]}:"
puts "\t[join [array names O [string map\
{[ ? ] ?} {[helpme]}]] \n\t]\n"
puts "note: -exact isn't an option for implementation,\
as the string i'm searching for is only a part of the\
array keys i'm searching for"
I don't quite understand your question. You have a bunch of code that
prints a bunch of output but I don't know what you think is valid and
what you think is invalid.
However, the problem appears to be a simple quoting problem. a)
remember that you have to protect [ and ] from the initial parsing of
the command line and b) [ and ] are special to the glob pattern
matcher so they have to be protected there, too.
Given that, the following returns a list of all keys that match the
literal string [helpme]:
array names 0 -glob {\[helpme\]}
and if you prefer quotes, the following also works but is less
readable:
array names 0 -glob "\\\[helpme\\\]"
Does that help?
yes, it does. effectively, i needed double quoting. thanks for
parsing my input and providing me with intelligent output.
On suggestion: instead of using double quoting, use brace quotes and
format. For example:
Instead of:
set foo "helpme"
puts "Pattern is \[$foo\]"
do:
set foo "helpme"
puts [format {Pattern is [%s]} $foo]
or use append to build your final string in pieces, as in:
set result {Pattern is [}
append result $foo {]}
Thus if you need a new line and a tab in there, lets say after the word is
you could do:
set result {}
append result {Pattern is} "\n\t" {[}
append result $foo {]}
I.e. only use quotes when required.
--
+------------------------------------------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
.. that's assuming I could control the placement of brackets -- I
don't control the input (the example above was only for illustrative
purposes). The input could have one or more brackets. I ended up
double-quoting (after first string mapping them to single-char glob
matches (?'s)).
\\ is actually double escaping. The " character is a double quote, as
I'm sure you know. I don't mean any disrespect in pointing this out;
I was just confused for a minute, trying to make sense of what you
wrote.
Yes and Quoting Hell should really be named Escaping Hell, but Orpheus
patented it first :P
-Alex
> Yes and Quoting Hell should really be named Escaping Hell, but Orpheus
> patented it first :P
QOTW-worthy!