Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Problem with using Symbol[] in a loop

  • To: mathgroup at smc.vnet.net
  • Subject: [mg111842] Re: Problem with using Symbol[] in a loop
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Sun, 15 Aug 2010 07:36:36 -0400 (EDT)

Easy answer: because in the first version you included Print but in the 
second you did not!  If you change to ...

   For[i = 0, i < Length[dirnames], i++; Print[Symbol[dirnames[[i]]]]]

... then you'll get the expected result.

The underlying reason -- from the ref page on For:

   Unless an explicit Return is used, the value returned by For is Null.

Without the Print wrapper, all you get is a Null value returned, which 
means nothing is printed as the output.

BTW, this, but usually for Do, is one of the more common questions we 
see in this news group.

On 8/13/2010 6:57 AM, Bob F wrote:
> Can anyone tell me why this works as expected:
>
>        dirnames = Names["$*Directory*"];
>        For[i = 0, i<  Length[dirnames], i++; Print[dirnames[[i]]]]
>
> and gives the following results:
>
>      $AddOnsDirectory
>
>      $BaseDirectory
        :
        :
>
> But that the following doesn't give any results:
>
>      dirnames = Names["$*Directory*"];
>      For[i = 0, i<  Length[dirnames], i++; Symbol[dirnames[[i]]]]
>
> but if I go thru the dirnames array elements manually it works as I
> would expect, e.g.
>
>      In[161]:= Symbol[dirnames[[1]]]
>
>      Out[161]= "/Library/Mathematica"
>
> Why does the Print[dirnames[[i]]] work OK but the
> Symbol[dirnames[[i]]] fail to give anything?
>
> Thanks...
>
> -Bob
>

-- 
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305


  • Prev by Date: Re: Equation style crash bug
  • Next by Date: Re: Syntax coloring
  • Previous by thread: Problem with using Symbol[] in a loop
  • Next by thread: Re: Can I set Page Setup scale for a notebook programmatically?