Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2007

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

Search the Archive

Re: Copy and Pasting Tables into Spreadsheet

  • To: mathgroup at smc.vnet.net
  • Subject: [mg83504] Re: Copy and Pasting Tables into Spreadsheet
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Wed, 21 Nov 2007 05:57:40 -0500 (EST)
  • References: <fhp2jd$1q6$1@smc.vnet.net> <fhu71j$70l$1@smc.vnet.net> <4742DFB0.20107@gmail.com> <fi0nnh$5gb$1@smc.vnet.net>

Gregory Lypny wrote:
>
> On 20-Nov-2007, at 8:22 AM, Jean-Marc Gulliet wrote:
> 
>> gregory.lypny at videotron.ca wrote:
>>
>> <snip>
>>
>>> Is there a way to tell
>>> Mathematica to return a scalar in all situations when the result  
>>> would
>>> otherwise be a list with only one element?
>> Gregory,
>>
>> 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}}
>>
 >
 > Thank you Jean-Marc,
 >
 > I'll look into this.  I'm also tinkering with a brute-force script
 > that will flatten and then partition any table to create one with a
 > depth of 3.  That should allow copying of numbers as plain text and
 > then pasting them into other applications.
 >

[note: swapped the quoted posts to keep chronological order]

Map[Flatten, data, {1}] will flatten out everything in 'data' above 
level 1.

The problem with *returning* a scalar in all cases when the result would 
be a single-element list is that return values cannot be precisely 
defined in Mathematica (because of how evaluation works).  The things 
"returned" by a function might be evaluated further.

The problem with the $Post approach is that it only works on the final 
result of an evaluation.  With the suggested $Post function, {2*3} 
evaluates to 6, but Table[{i j}, {i, 8}, {j, 8}] still evaluates to a 
{8, 8, 1} list.  It is a bit dangerous to use $Post in this way because 
it may deceive you (by altering results) and lead to more confusion.  If 
List weren't Locked, one could do List[{x_}] := x, but this would break 
lots and lots of things ...

Szabolcs


  • Prev by Date: Re: Importing and manipulating text files
  • Next by Date: Re: Neural networks with mathematica
  • Previous by thread: Re: Re: Copy and Pasting Tables into Spreadsheet
  • Next by thread: Re: Piecewise inside a Module or Block, I don't understand this