4

If you make a definition, for example

blah := RandomChoice[{1, 1, 1, 1, 1} -> {1000, 1001, 1002, 1003,1004}]

is there any way to programmatically use blah to get that second list as an output? Some pattern of the Definition or FullDefinition...?

I know I have the list right there, but later on, after defining blah, I'd like to grab that second list of numbers and use it.

Thanks for any insight you might provide.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Tom De Vries
  • 3,758
  • 18
  • 36
  • 2
    Take a look at OwnValues[blah] – Kuba Mar 04 '17 at 18:47
  • Why not just define val={1000, 1001, 1002, 1003, 1004} and then use this to define blah? – bill s Mar 04 '17 at 19:22
  • yes, absolutely, I am not so far along with this process that I can't tweak it a bit, I just thought I might learn something about getting at the parts of an expression, in this case, the definition . I had seen UpValues and DownValues, but @Kuba mentioned OwnValues, I had no idea... not possible to know every function, so I thought I would see there was a way, just "in theory"....No idea how to pull apart the result of OwnValues to get the pieces though....! – Tom De Vries Mar 04 '17 at 19:28

1 Answers1

7

As commented by Kuba you can extract the desired expression from OwnValues:

blah := RandomChoice[{1, 1, 1, 1, 1} -> {1000, 1001, 1002, 1003, 1004}];

OwnValues[blah][[1, 2, 1, 2]]
{1000, 1001, 1002, 1003, 1004}

my step function also works:

stepEval[blah][[1, 1, 2]]
{1000, 1001, 1002, 1003, 1004}

If you know the definition structure of blah, which it seems you must, you could also Block a la Convert head Times to List:

Block[{RandomChoice = # &, Rule = #2 &}, blah]
{1000, 1001, 1002, 1003, 1004}

For the future you might consider using this form instead:

blah2 = RandomChoice[{1, 1, 1, 1, 1} -> {1000, 1001, 1002, 1003, 1004}] &;

It would be used by calling blah2[]. And the part can be extracted with:

blah2[[1, 1, 2]]
{1000, 1001, 1002, 1003, 1004}
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371