Subject: Re: arrays really aren't - DN [1]


"Gerald W. Lester" <Gerald.Lester@cpu.com> - 14 May 1999 - comp.lang.tcl

 --------------C55DC3C16206280FD04EB843
 Content-Type: text/plain; charset=us-ascii
 Content-Transfer-Encoding: 7bit

 Phil wrote:

 > Donal K. Fellows <fellowsd@cs.man.ac.uk> wrote:
 > >In article <7hcfor$ani@gap.cco.caltech.edu>,
 > >Phil <pehrens@ligo.caltech.edu> wrote:
 > >> proc foo { bam } {
 > >>      array set ::bar {bim $bam}
 > >> }
 > >
 > >You really meant:
 > >    proc foo {bam} {array set ::bar [list bim $bam]}
 > >I forgive you though...  :^)
 >
 > Okay, my generality was suffering a lapse, I concede!
 >
 > >> Amusingly, things like $::var and ::$var are equivalent.
 > >
 > >WRONG!  They are most definitely not equivalent:
 > >
 > >    % proc foo {bar} {puts "$::bar - ::$bar"}
 > >    % set bar 1; foo 0
 > >    1 - ::0
 >
 > What about:
 >
 > set foo 12
 > set ::$foo twelve
 > set $foo --> twelve
 > set $::foo TWELVE
 > set $foo --> TWELVE
 >
 > Now it's even more disturbing, dont you think?

 Not at all! The only thing disturbing is you forgetting *where*
 substitutions happen at, i.e. in the parser.  Lets look at what the set
 command (at the C level, sees for its arguments) in each case:

 set foo 12

      Arg #1) foo
      Arg #2) 12

      Variable foo <= 12

 set ::$foo twelve

      Arg #1) ::12    (foo has already been substituted!)
      Arg #2) twelve

      Variable 12 <= twelve

 set $foo --> twelve

      Arg #1) 12 (foo has already been substituted!)

      Returns the value of variable 12, i.e. twelve

 set $::foo TWELVE

      Arg #1) 12    (::foo, which is the same as foo, has already
      been substituted!)
      Arg #2) TWELVE

      Variable 12 <= TWELVE

 set $foo --> TWELVE

      Arg #1) 12 (foo has already been substituted!)

      Returns the value of variable 12, i.e. TWELVE

 Tcl is very consistent, you just have to remember that it is interpretive
 and the parser does the substitutions.

 > The local variable "bar" in proc "foo" is not in the "" namespace
 > in proc "foo".  Where is it?
 >

 Here you appear be getting namespaces and scope (i.e. stack frames)
 confused. They are two completely different things.

 --
 +--------------------------------+---------------------------------------+

 | Gerald W. Lester               | "The man who fights for his ideals is
 |
 | gerald_lester@bellsouth.net    |  the man who is alive." -- Cervantes
 |
 |               Webmaster for http://www.mtolive-lcms.org
 |
 +--------------------------------+---------------------------------------+

 --------------C55DC3C16206280FD04EB843
 Content-Type: text/html; charset=us-ascii
 Content-Transfer-Encoding: 7bit

 <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
 <html>
 Phil wrote:
 <blockquote TYPE=CITE>Donal K. Fellows &lt;fellowsd@cs.man.ac.uk> wrote:
 <br>>In article &lt;7hcfor$ani@gap.cco.caltech.edu>,
 <br>>Phil &lt;pehrens@ligo.caltech.edu> wrote:
 <br>>> proc foo { bam } {
 <br>>>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array set ::bar {bim $bam}
 <br>>> }
 <br>>
 <br>>You really meant:
 <br>>&nbsp;&nbsp;&nbsp; proc foo {bam} {array set ::bar [list bim $bam]}
 <br>>I forgive you though...&nbsp; :^)
 <p>Okay, my generality was suffering a lapse, I concede!
 <p>>> Amusingly, things like $::var and ::$var are equivalent.
 <br>>
 <br>>WRONG!&nbsp; They are most definitely not equivalent:
 <br>>
 <br>>&nbsp;&nbsp;&nbsp; % proc foo {bar} {puts "$::bar - ::$bar"}
 <br>>&nbsp;&nbsp;&nbsp; % set bar 1; foo 0
 <br>>&nbsp;&nbsp;&nbsp; 1 - ::0
 <p>What about:
 <p>set foo 12
 <br>set ::$foo twelve
 <br>set $foo --> twelve
 <br>set $::foo TWELVE
 <br>set $foo --> TWELVE
 <p>Now it's even more disturbing, dont you think?</blockquote>
 Not at all! The only thing disturbing is you forgetting *where* substitutions
 happen at, i.e. in the parser.&nbsp; Lets look at what the set command
 (at the C level, sees for its arguments) in each case:
 <p>set foo 12
 <blockquote>Arg #1) foo
 <br>Arg #2) 12
 <p>Variable foo &lt;= 12</blockquote>
 set ::$foo twelve
 <blockquote>Arg #1) ::12&nbsp;&nbsp;&nbsp; (foo has already been substituted!)
 <br>Arg #2) twelve
 <p>Variable 12 &lt;= twelve</blockquote>
 set $foo --> twelve
 <blockquote>Arg #1) 12 (foo has already been substituted!)
 <p>Returns the value of variable 12, i.e. twelve</blockquote>
 set $::foo TWELVE
 <blockquote>Arg #1) 12&nbsp;&nbsp;&nbsp; (::foo, which is the same as foo,
 has already been substituted!)
 <br>Arg #2) TWELVE
 <p>Variable 12 &lt;= TWELVE
 <br>&nbsp;</blockquote>
 set $foo --> TWELVE
 <blockquote>Arg #1) 12 (foo has already been substituted!)
 <p>Returns the value of variable 12, i.e. TWELVE</blockquote>
 Tcl is very consistent, you just have to remember that it is interpretive
 and the parser does the substitutions.
 <br>&nbsp;
 <blockquote TYPE=CITE>
 <pre>The local variable "bar" in proc "foo" is not in the "" namespace
 in proc "foo".&nbsp; Where is it?</pre>
 </blockquote>

 <p><br>Here you appear be getting namespaces and scope (i.e. stack frames)
 confused. They are two completely different things.
 <p><tt>--</tt>
 <br><tt>+--------------------------------+---------------------------------------+</tt>
 <br><tt>| Gerald W. Lester&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 | "The man who fights for his ideals is |</tt>
 <br><tt>| gerald_lester@bellsouth.net&nbsp;&nbsp;&nbsp; |&nbsp; the man
 who is alive." -- Cervantes&nbsp; |</tt>
 <br><tt>|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 Webmaster for <A HREF="http://www.mtolive-lcms.org">http://www.mtolive-lcms.org</A>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 |</tt>
 <br><tt>+--------------------------------+---------------------------------------+</tt>
 <br>&nbsp;</html>

 --------------C55DC3C16206280FD04EB843--

Last modified
1999-09-27

(195.108.246.50)

Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated