MathGroup Archive 2007

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

Search the Archive

Re: Scalars Instead of Lists with One Element

  • To: mathgroup at smc.vnet.net
  • Subject: [mg83455] Re: Scalars Instead of Lists with One Element
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Wed, 21 Nov 2007 02:43:26 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <fhu75c$72j$1@smc.vnet.net>

Gregory Lypny wrote:

> Say I have the list x={2, 7}.  Is there a command I can use to get 2  
> rather than {2} when I ask for x[[1]], and 7 rather than {7} when I  

Here, I do not follow (or understand) you. You have a list of scalars 
and taking any one of its elements with *Part* will produce a scalar and 
not a list of one element. Or you may have something else in mind... For 
instance,

In[1]:= x = {2, 7}

Out[1]= {2, 7}

In[2]:= x[[1]]

Out[2]= 2

In[3]:= x[[2]]

Out[3]= 7

As you can see above, no list is returned in this case.

> ask for x[[2]]?  Doing so would solve my problem with my tables being  
> too deep to be able to cut and paste directly into the table objects  
> of other applications like word processors and spread sheets.

You could use the system variable *$Post* and set up your own post
processing function. (You might be interested in $PrePrint too.) Here is
an example of such a function that should do what you are looking for,
though you may want to add some additional tests (or remove few of them).

In[1]:= $Post =
   If[Head[#] === List && Length[#] == 1 &&
      Depth[#] < 3, #[[1]], #] &;

prob = {.4, {.6}, {.4, .6}, {{.4, .6}}};

prob[[1]] (* Scalar: not a list, depth one *)
prob[[2]] (* List of length one and depth two *)
prob[[3]] (* List of length two and depth two *)
prob[[4]] (* List of length one and depth three *)

$Post =. (* Reset $Post *)

Out[3]= 0.4

Out[4]= 0.6

Out[5]= {0.4, 0.6}

Out[6]= {{0.4, 0.6}}

Best regards,
-- 
Jean-Marc


  • Prev by Date: Re: Variable-length list?
  • Next by Date: Can DSolve handle normalized vectors?
  • Previous by thread: Re: Scalars Instead of Lists with One Element
  • Next by thread: Re: Scalars Instead of Lists with One Element