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

info exists array-element

1,055 views
Skip to first unread message

Vaclav Snajdr

unread,
May 22, 2001, 3:40:28 AM5/22/01
to
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 .....

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

snajdr.vaclav.vcf

Neil Madden

unread,
May 22, 2001, 4:55:30 AM5/22/01
to
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 ...


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

Vaclav Snajdr

unread,
May 22, 2001, 5:57:27 AM5/22/01
to
Fine, it runs well now, thanks.
snajdr.vaclav.vcf

Richard.Suchenwirth

unread,
May 22, 2001, 7:56:22 AM5/22/01
to
> * 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.
--
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

Bruce Hartweg

unread,
May 22, 2001, 11:32:06 AM5/22/01
to
"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
> (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


Jeff Hobbs

unread,
May 22, 2001, 12:41:28 PM5/22/01
to Richard.Suchenwirth
"Richard.Suchenwirth" wrote:
>
> > * From: Neil Madden <nem...@nottingham.ac.uk>
> > 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

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

Neil Madden

unread,
May 22, 2001, 8:18:36 PM5/22/01
to

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

Kevin Kenny

unread,
May 23, 2001, 7:14:17 PM5/23/01
to
Neil Madden wrote:
> > 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.

It's a bug, ok? Fixed in 8.3.3.

--

Mark Patton

unread,
May 27, 2001, 8:44:06 AM5/27/01
to
The problem with allocating memory for array elements with info exists
has been fixed for a while. Try the latest releases. I ran into the
problem when I was iterating over huge arrays in an NLP assignment.
Figuring out why I kept running out of memory took quite a while. :)

Mark

In article <3B0A8676...@raytheon.com>, "Bruce Hartweg"

* Tong *

unread,
Jun 3, 2001, 9:45:37 PM6/3/01
to
"Mark Patton" <mpa...@jhu.edu> writes:

> 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

0 new messages