4
$\begingroup$

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.

$\endgroup$
  • 2
    $\begingroup$ Take a look at OwnValues[blah] $\endgroup$ – Kuba Mar 4 '17 at 18:47
  • $\begingroup$ Why not just define val={1000, 1001, 1002, 1003, 1004} and then use this to define blah? $\endgroup$ – bill s Mar 4 '17 at 19:22
  • $\begingroup$ 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....! $\endgroup$ – Tom De Vries Mar 4 '17 at 19:28
7
$\begingroup$

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}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.