For Programmers: Free Programming Magazines  


Home > Archive > Tcl > February 2007 > Why does that not work :(









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Why does that not work :(
Andy1407

2007-02-20, 8:07 am

Hi everybody,

i use this code:
<---
set array(3) 4;
<---

but it allways breaks down...


when i use
<---
set array{3} 4;
<---

it runs throw...
but i could not get any value out of the array....


Why???
Whats the problem???


Thanks for answer(s)
Andy

Mark Janssen

2007-02-20, 8:07 am

On Feb 20, 11:06 am, "Andy1407" <Andreas.Hornda...@gmail.com> wrote:
> Hi everybody,
>
> i use this code:
> <---
> set array(3) 4;
> <---
>
> but it allways breaks down...
>
> when i use
> <---
> set array{3} 4;
> <---
>
> it runs throw...
> but i could not get any value out of the array....
>
> Why???
> Whats the problem???
>
> Thanks for answer(s)
> Andy


What would be essential to know in this case is what your error
message is. One reason you might see this behaviour is that array is
already defined as a scalar variable:

% set array 5
5
% set array(3) 4
can't set "array(3)": variable isn't array
%

The reason that with array{3} it might work in this case is that you
are then setting the variable with name "array{3}" but that is tricky
to get the value from as you found out.

% set array{3} 4
4
(bin) 58 % puts [set array{3}]
4

The only way to know for sure is if you post the error you are getting
though.

Mark

Andy1407

2007-02-20, 8:07 am

Hi Mark,

i think i know what you mean....

how could i declear an empty array on the start of my tcl script??


andy

On 20 Feb., 11:15, "Mark Janssen" <mpc.jans...@gmail.com> wrote:
> On Feb 20, 11:06 am, "Andy1407" <Andreas.Hornda...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
> What would be essential to know in this case is what your error
> message is. One reason you might see this behaviour is that array is
> already defined as a scalar variable:
>
> % set array 5
> 5
> % set array(3) 4
> can't set "array(3)": variable isn't array
> %
>
> The reason that with array{3} it might work in this case is that you
> are then setting the variable with name "array{3}" but that is tricky
> to get the value from as you found out.
>
> % set array{3} 4
> 4
> (bin) 58 % puts [set array{3}]
> 4
>
> The only way to know for sure is if you post the error you are getting
> though.
>
> Mark- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -



Bryan Oakley

2007-02-20, 8:07 am

Andy1407 wrote:
> Hi Mark,
>
> i think i know what you mean....
>
> how could i declear an empty array on the start of my tcl script??
>


array set myArray {}
Arjen Markus

2007-02-20, 8:07 am

On 20 feb, 12:43, "Andy1407" <Andreas.Hornda...@gmail.com> wrote:
> Hi Mark,
>
> i think i know what you mean....
>
> how could i declear an empty array on the start of my tcl script??
>


That is easy enough:

> array set myarray {}


> set myarray 1

can't set "myarray": variable is array


Regards,

Arjen

Massimo Manghi

2007-02-20, 7:07 pm

Bryan Oakley wrote:
> Andy1407 wrote:
>
> array set myArray {}


if you have set myArray prior to that line that might not work as
a sort of declaration. It simply means "add no new variable to myArray"

example

set myArray(1) 10
array set myArray {}
array get myArray
=> 1 10

this might have benign side effects, but it might also confuse the
debugging, it leaves garbage around and, in the worst cases, breaks
the code.

I don't have a perfect solution for that. I usually adopt
a radical approach: "array unset myArray", which
doesn't raise an error even if myArray is not existing and
makes the way clear for creating a brand new array (provided
no regular variable called myArray is around)

-- Massimo Manghi
Cameron Laird

2007-02-20, 7:07 pm

In article <1171973146.241614.274560@p10g2000cwp.googlegroups.com>,
Arjen Markus <arjen.markus@wldelft.nl> wrote:
>On 20 feb, 12:43, "Andy1407" <Andreas.Hornda...@gmail.com> wrote:

Ralf Fassel

2007-02-20, 7:07 pm

* Massimo Manghi <manghi@biol.unipr.it>
| >> how could i declear an empty array on the start of my tcl
| >> script??
| >>
| > array set myArray {}
|
| if you have set myArray prior to that line ...

The code was supposed to go "on the start of [the] tcl script".
So there should be no problem with myArray prior to that line.

| that might not work as a sort of declaration. It simply means "add
| no new variable to myArray"

And it prevents using myArray as plain variable later, which is what
the OP wanted to prevent problems using it as array even later.

% array set myArray {}
% set myArray {}
=> can't set "myArray": variable is array

R'
Ralf Fassel

2007-02-20, 7:07 pm

* "Andy1407" <Andreas.Horndasch@gmail.com>
| Whats the problem???

The problem is that you did not show the error message you get.
Without the exact error message one can not even guess what the
problem in your script might be.

R'
Bryan Oakley

2007-02-20, 7:07 pm

Massimo Manghi wrote:
> Bryan Oakley wrote:
>
> if you have set myArray prior to that line that might not work as
> a sort of declaration. It simply means "add no new variable to myArray"
>
> example
>
> set myArray(1) 10
> array set myArray {}
> array get myArray
> => 1 10
>


Right. But he said "... declare an empty array on the start of my tcl
script" which is what my suggestion does.

You are correct that "array set myArray {}" does not *reset* a variable
to be an empty array, it simply initializes a non-existant variable as
an array.



--
Bryan Oakley
http://www.tclscripting.com
Donal K. Fellows

2007-02-20, 7:07 pm

Cameron Laird wrote:
> Notice, by the way, that it's very rare for a "declaration"
> to be good style in Tcl.


No, it's actually good style for variables that are accessed outside
the current procedure. Use the [global] or [variable] commands to do
it. Indeed, do not access a variable in a namespace scope without
first "declaring" it with [variable] or you'll be vulnerable to one of
the few true misfeatures of Tcl (the variable resolution system!)

Donal (been hurt by this more than once...)

Larry W. Virden

2007-02-20, 7:07 pm

On Feb 20, 5:06 am, "Andy1407" <Andreas.Hornda...@gmail.com> wrote:

> i use this code:
> <---
> set array(3) 4;
> <---
>
> but it allways breaks down...


_allways_? Do you mean to say that each and every time you attempt to
set a value into the variable, it fails?

>
> when i use
> <---
> set array{3} 4;
> <---
>
> it runs throw...
> but i could not get any value out of the array....
>
> Why???
> Whats the problem???



Well, if the set fails, that would be why you didn't get a value out
of the array.

I'm going to use http://tip.tcl.tk/131 features here... if you look
through your entire application, you will find at least one of the
following things occurring:

setting the array member inside a proc, without marking the array as
being from a specific namespace (global, whatever).

setting a variable with the same name as the array, without
appropriate indication of the namespace to use for the variable.

attempting to set, or access, a variable from some sort of widget or
callback , without the necessary scope/namespace indicator.

one or more references to the array that have a 'misspelling' in
either the array name or the member name (misspelling might include
extra white space, etc.)

These are probably my most frequent errors within more complicated tcl
code.




Massimo Manghi

2007-02-20, 7:07 pm

Bryan Oakley wrote:
>
> Right. But he said "... declare an empty array on the start of my tcl
> script" which is what my suggestion does.
>
> You are correct that "array set myArray {}" does not *reset* a variable
> to be an empty array, it simply initializes a non-existant variable as
> an array.
>
>
>

True, I overlooked that line, but I've been coding so intensely with
Tcl lately (making a wide usage of arrays) that as soon as I saw
that initialization I could not resist.

-- M
MartinLemburg@UGS

2007-02-20, 7:07 pm

Hello Andy,

just a question at the beginning. Did you have ...

set array{3} 4

.... in your code? With that braces around the "3" instead of
paranthesis?

Yes?

Than it will run through, because you just create a variable
with the name "array{3}".
And "array{3}" is a valid variable name, even if it can only
be accessed by ...

% puts [set array{3}];
4

No?

Than it is about a "scalar", a normal variable with the
misleading name "array", that is already in use and so can't
be used as an array.

Best regards,

Martin Lemburg
UGS - a Siemens Company - Transforming hte Process of Innovation
On Feb 20, 11:06 am, "Andy1407" <Andreas.Hornda...@gmail.com> wrote:
> Hi everybody,
>
> i use this code:
> <---
> set array(3) 4;
> <---
>
> but it allways breaks down...
>
> when i use
> <---
> set array{3} 4;
> <---
>
> it runs throw...
> but i could not get any value out of the array....
>
> Why???
> Whats the problem???
>
> Thanks for answer(s)
> Andy



Larry W. Virden

2007-02-20, 7:07 pm

On Feb 20, 11:43 am, "MartinLemburg@UGS" <martin.lemburg....@gmx.net>
wrote:
> Hello Andy,
>
> just a question at the beginning. Did you have ...
>
> set array{3} 4
>
> ... in your code? With that braces around the "3" instead of
> paranthesis?


So, what tools could the original poster put to work that would assist
him/her in locating this kind of problem? Sometimes things like this
are overlooked for a time because it sits in the middle of otherwise
correct code. Tcl itself won't warn the user, because it is a valid
construct. Something stylistically oriented, though, might comment on
the peculiar notation...

MartinLemburg@UGS

2007-02-20, 7:07 pm

Hi Larry,

> So, what tools could the original poster put to work that would assist
> him/her in locating this kind of problem?


For my and my colleagues work I wrote a kind of documentation tool,
creating metrics, checking code for critical things via REs and
extracting (API) documentation.

But ... there is no free official tool for such problems.

I even don't know if the Tcl Pro or TDK tool "procheck" would find
such problems.

Best regards,

Martin Lemburg
UGS - a Siemens Company - Transforming hte Process of Innovation

On Feb 20, 5:55 pm, "Larry W. Virden" <lvir...@gmail.com> wrote:
> On Feb 20, 11:43 am, "MartinLemburg@UGS" <martin.lemburg....@gmx.net>
> wrote:
>
>
>
>
>
> So, what tools could the original poster put to work that would assist
> him/her in locating this kind of problem? Sometimes things like this
> are overlooked for a time because it sits in the middle of otherwise
> correct code. Tcl itself won't warn the user, because it is a valid
> construct. Something stylistically oriented, though, might comment on
> the peculiar notation...



Donal K. Fellows

2007-02-21, 8:05 am

Martin Lemburg wrote:
> For my and my colleagues work I wrote a kind of documentation tool,
> creating metrics, checking code for critical things via REs and
> extracting (API) documentation.


It would be nice if there was a standard form for documentation
comments, and a set of standard library scripts for extracting the
information from the comments. Like that, we could have both API
describers and other related tools that work with script metadata.

Consider this a call for someone to come up with smart ideas. :-)

Donal.

Larry W. Virden

2007-02-21, 8:05 am

On Feb 21, 8:06 am, "Donal K. Fellows" <donal.k.fell...@man.ac.uk>
wrote:
> It would be nice if there was a standard form for documentation
> comments, and a set of standard library scripts for extracting the
> information from the comments. Like that, we could have both API
> describers and other related tools that work with script metadata.
>
> Consider this a call for someone to come up with smart ideas. :-)


I can't claim _smart_ ideas ;-) , but here's some thoughts.

If you want to simply extract documentation, without manipulation or
reuse of the data, then something simple can be done - see perl's POD
notation (or see Sdoc, which helps you add doc to your source),
Andreas Kupries or Joerg Reiberg's AutoDoc, HelpSystem, robodoc, tdoc,
TL, tna, zdoc and probably other such things


Larry W. Virden

2007-02-21, 8:05 am

On Feb 21, 8:06 am, "Donal K. Fellows" <donal.k.fell...@man.ac.uk>
wrote:

> Consider this a call for someone to come up with smart ideas. :-)



Whoops - guess that proves my point :blush:

Anyways, if you want to make use of a program's documentation, with
semantical
meaning added to the code, then your choices are to a) trust the
programmer (not really a safe thing) or b) implement tools and a
standard documentation form that forces the coder to supply
information in some kind of fixed notation, with a 'policing' checking
action taken by the tools to force the coder to do what is needed.

The problem with a policing direction is that the lazy programmer
(which includes most, if not all, of them...) will find ways around
the system - by either leaving fields blank, typing in "N/A", or copy
and pasting text regardless of relevancy, etc.

Sigh.

Donal K. Fellows

2007-02-21, 7:15 pm

Larry W. Virden wrote:
> Anyways, if you want to make use of a program's documentation, with
> semantical meaning added to the code, then your choices are to a) trust
> the programmer (not really a safe thing) or b) implement tools and a
> standard documentation form that forces the coder to supply
> information in some kind of fixed notation, with a 'policing' checking
> action taken by the tools to force the coder to do what is needed.


No system is perfect, true. But should we let the imperfection stop
us? Right now, it comes across like you're advocating "c) do nothing
and keep wringing our hands over this because it is 'too hard to do
perfectly'." Remember, the status quo is distinctly sub-optimal even
within the space of what is practical...

Donal.

MartinLemburg@UGS

2007-02-21, 7:15 pm

Hi Larry,

you are right.

Looking at API documentations extracted from header files or modules
in C(++) I find so different qualities of documentation, that I
sometimes think I'm going crazy.

One big, big part of my work is to analyse "foreign" APIs to find out,
if they are capable for the things we need and than to encaspulate
them for our use, even perhabs for the usage in tcl.

I thought of using the doctools from tcllib or javadoc, or ... to
create a good documentation, but I felt not well with those
approaches, or I simply felt like not understanding the capabilities
of those approaches, had fear the complexity and decided to some more
complex - writing our own documentation tool.

So I only had to force myself to document each file I touch or create
in the following manner (no matter if in C(++) or tcl):

1. each file gets a header, with a "long name", a description, a
maintainer and manager name, a list of dependencies or related files/
modules, and a history (our CVS like system does not create a history
on check-in).

2. every "exported" function/procedure gets a header like:

# syntax: UIFileOpenDlg ?option value?
# result: a file name or an empty string on cancel
# details: this ...
# dependencies: tk_getOpenFile, tk_messageBox
# related: UIFileSaveDlg
#
proc ::UIFileOpenDlg ...

3. every "exported" function/procedure header can have additionally a
maintainer, manager and a history, too

Our documentation tool simply goes through each source file in a
recursive directory scan and looks for tokens, like mentioned above.
It extracts those information/data following the found tokens
Afterwards it creates HTML pages with crossreferences for each scanned
module/file.

Additionally the documentation tool looks with REs for:

1. "symbols" like e.g. "Tcl_.*", to create an symbol occurrences
statistics
2. critical code segments (to-don'ts), like e.g. "info exists $var",
to create a "warnings" statistic and documentation per module/file
3. calculates for each function/procedure the cyclomatic complexity,
to create a complexity statistic per module/file and over all modules/
files
4. extracts some other metrics like line counts, comment occurrences
and complexity, coverage of code, function/procedure bodies, comments
in relation to source lines

All the results of the documentation tool are crossreferenced HTML
pages (thanks to htmlgen from xmlgen).

The developer is able to use some basic text formats like in a wiki,
so that **...** creates bold text, ..., @@...@@ creates a reference to
a symbol, module or documentated function/procedure or a link with the
allowed URL schemes https?, ftp, mailto, file. Even nested (un)ordered/
definition lists are possible.

The whole documentation tool exists as starpack with a Tk UI and a
complex commandline, so that everybody from our team can use it in his
manner.

For our usage, this tool is really fine.

But it is not the sometimes really needed syntax checker, and it
stricly depends on the discipline of any developer using this tool, to
really create good documentations.

Best regards,

Martin Lemburg
UGS - a Siemens Company - Transforming the Process of Innovation

On Feb 21, 2:54 pm, "Larry W. Virden" <lvir...@gmail.com> wrote:
> On Feb 21, 8:06 am, "Donal K. Fellows" <donal.k.fell...@man.ac.uk>
> wrote:
>
>
> Whoops - guess that proves my point :blush:
>
> Anyways, if you want to make use of a program's documentation, with
> semantical
> meaning added to the code, then your choices are to a) trust the
> programmer (not really a safe thing) or b) implement tools and a
> standard documentation form that forces the coder to supply
> information in some kind of fixed notation, with a 'policing' checking
> action taken by the tools to force the coder to do what is needed.
>
> The problem with a policing direction is that the lazy programmer
> (which includes most, if not all, of them...) will find ways around
> the system - by either leaving fields blank, typing in "N/A", or copy
> and pasting text regardless of relevancy, etc.
>
> Sigh.



Larry W. Virden

2007-02-21, 7:15 pm

On Feb 21, 9:24 am, "Donal K. Fellows" <donal.k.fell...@man.ac.uk>
wrote:
> It comes across like you're advocating "c) do nothing
> and keep wringing our hands over this because it is 'too hard to do
> perfectly'." Remember, the status quo is distinctly sub-optimal even
> within the space of what is practical...


My intent wasn't to wring our hands, but proceed with reasonable
expectations.
There won't be a perfect case. There are, however, as I mentioned in
the first part of my post, a dozen or more efforts, in the tcl
community, to provide programs which extract documentation.

If more than extraction is needed - something, perhaps, more along the
lines of literate programming (see Purchase for one example of this),
then more effort will be necessary. And more work will be required if
the intent is to make productive use of the semantic aspects of the
doc. HOWEVER, if the usefulness of the tools become great enough, that
becomes a motivating factor to ''do things right''.



Larry W. Virden

2007-02-21, 7:15 pm

On Feb 21, 9:28 am, "MartinLemburg@UGS" <martin.lemburg....@gmx.net>
wrote:
> One big, big part of my work is to analyse "foreign" APIs to find out,
> if they are capable for the things we need


Analyzing the code of others is painful, isn't it? Unless the person
is one of those obsessive closet technical editor types, the result is
typically a landmine filled landscape, ranging from small dribbles of
useless documentation (aka:

incr i ;# increment the counter

to doc that is obsolete, to doc that is useful...


>
> not understanding the capabilities
> of those approaches


Yes, this is one of the keys - the documentation method needs to be
nearly
transparent - the more that the tool can derive from the source, the
more likely the documentation will be current when one runs the tools
against the source.




> So I only had to force myself to document each file I touch or create
> in the following manner (no matter if in C(++) or tcl):


: some common sense rules

> Our documentation tool simply goes through each source file in a
> recursive directory scan and looks for tokens,


[...]

> Additionally the documentation tool looks with REs for:

:
good stuff
:

Probably some of the best things a programming group could implement
to improve the documentation of the code being generated would be to
have tools which help the documentor by extracting useful info,
skeleton/templates of the type of information that would be useful, as
well as some sort of mentoring/review
process which looks over what is generated and provides feedback
asking about why particular assumptions were made, etc.

Darren New

2007-02-21, 7:15 pm

Donal K. Fellows wrote:
> It would be nice if there was a standard form for documentation
> comments, and a set of standard library scripts for extracting the
> information from the comments. Like that, we could have both API
> describers and other related tools that work with script metadata.


The problems I see with this very idea applied to Tcl are these:

1) If you're talking about APIs, are you telling me you don't write the
documentation until you're already coding? That's bad news right there.

2) We already have such a system. Tcllib uses it. It works, and it's
high enough level that you can make multiple kinds of documentation out
of it. The documentation just isn't stuffed in the middle of the code.

3) Tcl doesn't have sophisticated parameter-parsing built in like other
languages do. If my proc takes {args} as its argument and does all its
own processing, there isn't going to be anything a tool can extract from
the source code that will be useful enough to warrant trying to
magically format comments inline. At the same time, Tcl does some funky
stuff that no static code is going to document, like creating a new
procedure each time you invoke "toplevel" or "button". Where would you
put the inline documentation for the "widget commands"?

--
Darren New / San Diego, CA, USA (PST)
"I'm getting addicted to feet. Which is
bad, because they're like $5 a pound."
Fredderic

2007-02-21, 10:08 pm

On Tue, 20 Feb 2007 14:50:10 +0100,
Ralf Fassel <ralfixx@gmx.de> wrote:

> | that might not work as a sort of declaration. It simply means "add
> | no new variable to myArray"
> And it prevents using myArray as plain variable later, which is what
> the OP wanted to prevent problems using it as array even later.


Now, now... None of this bickering.

Besides, [array unset] appears to do about didly squat to a simple
variable;

% set junk crapola
crapola
% array unset junk
% set junk
crapola

So... To put you both at ease;

proc declare_array {arrayName} {
upvar 1 $arrayName array
catch {unset array}
array set array {}
}

There. That should keep you both happy. ;)


Fredderic
Glenn Jackman

2007-02-22, 7:12 pm

At 2007-02-21 10:54PM, "Fredderic" wrote:
> On Tue, 20 Feb 2007 14:50:10 +0100,
> Ralf Fassel <ralfixx@gmx.de> wrote:
>
>
> Now, now... None of this bickering.


Heh, if you think *that's* bickering, hang out in comp.lang.perl.misc
for a few days...

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Fredderic

2007-02-24, 4:20 am

On 22 Feb 2007 14:25:44 GMT,
Glenn Jackman <glennj@ncf.ca> wrote:

> Heh, if you think *that's* bickering, hang out in comp.lang.perl.misc
> for a few days...


Nah... I get enough of it in a GTK newsgroup I mostly lurk in...
Some software developers get really rather snotty when you suggest that
someone should have had the foresight to make sure that their example
code for the library they're putting out (be it maintainer or packager),
is in the documentation packages instead of the source packages, which
by bizarre chance, you don't need unless you're hacking around within
the library itself. And as we all know EVERY _application_ developer
should do that on a regular basis. Especially when the library is part
a friggin' RAD tool, which exists primarially so that those very same
_application_ developers don't HAVE to go delving into the bowls of
scary libraries such as that one in the first place! It'd be like
asking someone writing TK scripts to look in the X server source for
documentation on a configuration option to a particular widget.

Okay. I'm over it. ;)


Fredderic
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com