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

• To: mathgroup at smc.vnet.net
• Subject: [mg68483] 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:52 -0400 (EDT)
• References: <eauvns\$17c\$1@smc.vnet.net> <200608060656.CAA23436@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```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" ?
>>
>>
>>
>>
>>
>>
>> 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

```

• Prev by Date: Re: PiecewiseExpand bug?
• Next by Date: Two strange problems with a notebook...
• Previous by thread: Re: returning a variable's name, rather than the variable's contents
• Next by thread: Re: Re: returning a variable's name, rather than the variable's contents