ClearAll[matchType];
SetAttributes[matchType, HoldFirst]
matchType[
type_Symbol,
candidates_?(VectorQ[#, StringQ] &)] :=
Module[
{typeStr = SymbolName[Unevaluated[type]]},
Select[
DeleteCases[candidates, typeStr],
ToExpression[# <> "==" <> typeStr] &]]
GrassA = 1;
GrassB = 2;
GrassC = 2;
This last entry could either be an error or a form of naming conflict caused by using capital letters at the start of user-defined names.
GrassType = GrassA;
matchType[GrassType, {"GrassA", "GrassB"}]
{GrassA}
Or to drop the list brackets
matchType[GrassType, {"GrassA", "GrassB"}][[1]]
GrassA
matchType[GrassType, Names["Grass*"]]
{GrassA}
GrassType = GrassB;
matchType[GrassType, {"GrassA", "GrassB"}]
{GrassB}
matchType[GrassType, Names["Grass*"]]
{GrassB,GrassC}
This last example detects the ambiguity or name similarity. This is why it is best (my opinion) to drop the list brackets outside the function rather than inside.
Bob Hanlon
---- Adam <juneappal at gmail.com> wrote:
=============
Hello -
Supposing I have declared:
x = 1
y = x
Is there a function that can take y as an argument and return "x"?
I would like to use it like this:
***
GrassA = 1
GrassB = 2
GrassType = GrassA (*<---- I will toggle this for different model
runs*)
(*Lots of code and calculations, followed by summary printout *)
Print[Row[{"GrassType = ", HOLDLIKEFUNCTION[GrassType], "\tDepth = ",
h}]]
*****
HoldForm[GrassType] returns "GrassType," instead of the desired
"GrassA."
For now, I am using:
******
Clear[GrassA, GrassB]
Print[Row[{"GrassType = ", HOLDLIKEFUNCTION[GrassType], "\tDepth = ",
h}]]
******
Which has the desired effect, so I am not desparate for a solution -
just wonder how other people would approach this.