set i [expr $i + 1]
set var $wef($i)
set da [info exists var]
if {$da == 1} { ... okay .....
sure if i = 10 an error
if I test with
set da [info exists $wef($i)]
I receive the same error
Please what is the correct syntax.
Thanks
Vaclav
Just do:
if {[info exists wef($i)]} { ... okay ...
The problem is that the "set var $wef($i)" line substitutes in the value
of the variable, before you have decided whether it exists. Then in the
"set da [info exists var]" line you are testing whether the variable
"var" exists, which it does because you've just set it in the previous
line! The equivalent would be:
incr i ;# Better way of saying "set i [expr $i +1]"
set var wef($i)
set da [info exists $var]
if {$da == 1} { .. okay ...
But beware that [info exists arrayelement] creates that array element
(and marks it as nonexistent), so if you're asking for many nonexistent
elements, it will silently grow in memory. To test with
if {[array names wef $i] != ""} {...
does not have that problem. And if there is a slight possibility that
the key string may contain glob-relevant characters (like *, ?,...), it
it safest to test
if {[array names wef $i] == $i} {...
to prevent false positives.
--
Schoene Gruesse/best regards, Richard Suchenwirth - +49-7531-86 2703
Siemens Dematic AG, PA RC D2, Buecklestr.1-5, 78467 Konstanz,Germany
Personal opinions expressed only unless explicitly stated otherwise.
--
Posted from sneak.kst.siemens.de [193.98.144.2]
via Mailgate.ORG Server - http://www.Mailgate.ORG
> > * From: Neil Madden <nem...@nottingham.ac.uk>
> > Vaclav Snajdr wrote:
> > > Tcl-Version 8.3, there is an array names wef contains 9 elements.
> > > I test if the array element exist with
> > > set i [expr $i + 1]
> > > set var $wef($i)
> > > set da [info exists var]
> > > if {$da == 1} { ... okay .....
> >
> > Just do:
> > if {[info exists wef($i)]} { ... okay ...
> >
>
> But beware that [info exists arrayelement] creates that array element
> (and marks it as nonexistent), so if you're asking for many nonexistent
> elements, it will silently grow in memory. To test with
> if {[array names wef $i] != ""} {...
> does not have that problem. And if there is a slight possibility that
> the key string may contain glob-relevant characters (like *, ?,...), it
> it safest to test
> if {[array names wef $i] == $i} {...
> to prevent false positives.
> --
But also be aware that for large arrays this method becomes very slow
results from 8.3.3 running on an 866 Xeon running NT
on 1,000 element array:
info exists method ~7 microseconds
array names variant ~125 microseconds
on 10,000 element array
info exists method ~7 microseconds
array names variant ~4000 microseconds
Bruce
This is no longer valid in 8.3.3+.
--
Jeff Hobbs The Tcl Guy
Senior Developer http://www.ActiveState.com/
Tcl Support and Productivity Solutions
"Richard.Suchenwirth" wrote:
>
> > * From: Neil Madden <nem...@nottingham.ac.uk>
> > Vaclav Snajdr wrote:
> > > Tcl-Version 8.3, there is an array names wef contains 9 elements.
> > > I test if the array element exist with
> > > set i [expr $i + 1]
> > > set var $wef($i)
> > > set da [info exists var]
> > > if {$da == 1} { ... okay .....
> >
> > Just do:
> > if {[info exists wef($i)]} { ... okay ...
> >
>
> But beware that [info exists arrayelement] creates that array element
It does?? That seems very odd behaviour. Why is this? That seems like a
nasty gotcha to me.
> (and marks it as nonexistent), so if you're asking for many nonexistent
> elements, it will silently grow in memory. To test with
> if {[array names wef $i] != ""} {...
> does not have that problem. And if there is a slight possibility that
> the key string may contain glob-relevant characters (like *, ?,...), it
> it safest to test
> if {[array names wef $i] == $i} {...
> to prevent false positives.
> --
> Schoene Gruesse/best regards, Richard Suchenwirth - +49-7531-86 2703
> Siemens Dematic AG, PA RC D2, Buecklestr.1-5, 78467 Konstanz,Germany
> Personal opinions expressed only unless explicitly stated otherwise.
>
> --
> Posted from sneak.kst.siemens.de [193.98.144.2]
> via Mailgate.ORG Server - http://www.Mailgate.ORG
--
--------------------------------------------------------------
Neil Madden. |
Personal: | Computer Science:
ne...@tallniel.co.uk | nem...@cs.nott.ac.uk
http://www.tallniel.co.uk | http://www.cs.nott.ac.uk/~nem00u
--------------------------------------------------------------
It's a bug, ok? Fixed in 8.3.3.
--
Mark
In article <3B0A8676...@raytheon.com>, "Bruce Hartweg"
> The problem with allocating memory for array elements with info exists
> has been fixed for a while.
[...]
Which version? 8.0? 8.3? thanks.
--
Tong (remove underscore(s) to reply)
*niX Power Tools Project: http://xpt.sourceforge.net/
http://members.xoom.com/suntong001/
- All free contribution & collection