MathGroup Archive 2006

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

Search the Archive

Re: Re: returning a variable's name, rather than the variable's contents

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68484] Re: [mg68461] Re: returning a variable's name, rather than the variable's contents
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Mon, 7 Aug 2006 01:40:55 -0400 (EDT)
  • References: <eauvns$17c$1@smc.vnet.net> <200608060656.CAA23436@smc.vnet.net> <999EBD9F-124E-4F23-A9CF-56B249DBD8D5@mimuw.edu.pl>
  • Sender: owner-wri-mathgroup at wolfram.com

On 6 Aug 2006, at 23:39, Andrzej Kozlowski wrote:

>
> On 6 Aug 2006, at 08:56, Peter Pein wrote:
>
>> Michael Stern schrieb:
>>> There must be a simple way to do this but it eludes me. Take for  
>>> example the
>>> following:
>>>
>>>
>>>
>>> In[2]:= a=1;b=2;c=3;
>>>
>>>
>>>
>>> In[3]:= Max[a,b,c]
>>>
>>>
>>>
>>> Out[3]= 3
>>>
>>>
>>>
>>> What would I do if I wanted Out[3] to equal "c" ?
>>>
>>>
>>>
>>> Thanks in advance,
>>>
>>>
>>>
>>> Michael Stern
>>>
>>>
>>>
>>
>> Hi Michael,
>>
>>   I'm not familiar with the string functions in Mathematica.  
>> Therefore a much simpler approach will exist; but this one works:
>>
>> In[1]:=
>> Attributes[nameOfMax] := {HoldAll};
>> nameOfMax[l__] :=
>>    Module[{ls = StringJoin[", ", StringDrop[StringDrop[ToString 
>> [Unevaluated[l]], 9], -1], ","], sp},
>>      sp = First /@ StringPosition[ls, ","];
>>      StringTake[ls, sp[[{#1, #1 + 1}]] + {2, -1}]& [Last[Ordering 
>> [{l}]]]]
>> In[3]:=
>> {a, b, c, huge} = {1, 2, 3, 10^10};
>> In[4]:=
>> nameOfMax[a, b, c]
>> Out[4]=
>> "c"
>> In[5]:=
>> nameOfMax[c, huge, a, b]
>> Out[5]=
>> "huge"
>>
>> HTH,
>>    Peter
>>
>
>
> One can achieve a similar effect without using any strings at all.
>
> Attributes[nameOfMax] := {HoldAll};
>
> nameOfMax[p__] := With[ {l = Ordering[Flatten[List @@
>         OwnValues /@ HoldForm[
>           p]], -1, #1[[2]] <= #2[[2]] &]}, HoldForm[a, b,  huge, c] 
> [[l]]]
>
> {a, b,  huge, c} = {1, 2, 10^10, 3};
>
>
> nameOfMax[a, b,  huge,c]
>
> huge
>
>
> Andrzej Kozlowski

Sorry, the correct code is of course:


Attributes[nameOfMax] := {HoldAll};

nameOfMax[p__] := With[ {l = Ordering[Flatten[List @@
         OwnValues /@ HoldForm[
           p]], -1, #1[[2]] <= #2[[2]] &]}, HoldForm[p][[l]]]


Andrzej Kozlowski





  • Prev by Date: Two strange problems with a notebook...
  • Next by Date: Re: critical points of a third order polynomial fit (simplification)
  • Previous by thread: Re: Re: returning a variable's name, rather than the variable's contents
  • Next by thread: AuthorTools/MakeContents