Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: array names and brackets

31 views
Skip to first unread message
Message has been deleted

wjw

unread,
Oct 5, 2009, 12:45:27 PM10/5/09
to
On Oct 5, 11:38 am, wjw <williamjw...@gmail.com> wrote:
> There is a decent possibility that I am Missing Something(tm), but I
> am having issues with getting array names results with glob strings
> containing brackets.  I would appreciate illumination.
>
> Here is a script that illustrates the issue:
>
line break goofs removed:

#!/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"

Bryan Oakley

unread,
Oct 5, 2009, 1:05:16 PM10/5/09
to
On Oct 5, 11:45 am, wjw <williamjw...@gmail.com> wrote:
> On Oct 5, 11:38 am, wjw <williamjw...@gmail.com> wrote:> There is a decent possibility that I am Missing Something(tm), but I
> > am having issues with getting array names results with glob strings
> > containing brackets. I would appreciate illumination.

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?

wjw

unread,
Oct 5, 2009, 1:17:23 PM10/5/09
to

yes, it does. effectively, i needed double quoting. thanks for
parsing my input and providing me with intelligent output.

Gerald W. Lester

unread,
Oct 5, 2009, 3:02:04 PM10/5/09
to

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|
+------------------------------------------------------------------------+

wjw

unread,
Oct 5, 2009, 5:35:18 PM10/5/09
to

.. 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)).

Aric

unread,
Oct 5, 2009, 6:51:14 PM10/5/09
to

\\ 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.

Alexandre Ferrieux

unread,
Oct 6, 2009, 2:25:22 AM10/6/09
to
On Oct 6, 12:51 am, Aric <aric.bi...@gmail.com> wrote:
>
> > .. 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

John Seal

unread,
Oct 6, 2009, 7:54:50 AM10/6/09
to
"Alexandre Ferrieux" <alexandre...@gmail.com> wrote in message
news:5fbe14f1-f299-4730...@o21g2000vbl.googlegroups.com...

> Yes and Quoting Hell should really be named Escaping Hell, but Orpheus
> patented it first :P

QOTW-worthy!


0 new messages