# Matching symbols in assignments using NotebookImport

How can symbols from assignments, ie left hand side in expressions Set and SetDelayed be matched after importing using NotebookImport?

Consider a minimal notebook with these mixed definitions:

selPos = Select[#>0&];
selNeg[x_]:= Select[x<0];
selNegPair[x_, y_] := Select[x < 0 && y < 0];


There may be more involved definitions with SubValues, UpValues etc but these are the main cases to solve for. Note the typical use of CompoundExpression.

After importing via NotebookImport using the "Input" option:

exampleInput = NotebookImport["Example.nb","Input"]


{HoldComplete[selPos=Select[#1>0&];],HoldComplete[selNeg[x_]:=Select[x<0];],HoldComplete[selNegPair[x_,y_]:=Select[x<0&&y<0];]}

Note the assignments are wrapped in HoldComplete (rather than HoldPattern output by OwnValues - is there an option for NotebookImport that will preserve the OwnValues?)

Is there an easier way to extract these symbols than to use 2 replaces and release hold? first to match the left hand of Set or SetDelayed, and second to extract the symbol from the HoldComplete, it seems to work only if HoldForm is used in the replacement rule, and finally hold is released:

exampleInput /. {HoldPattern[Set[f_,_]]:> f,HoldPattern[SetDelayed[f_,_]]:> f}/. {HoldComplete[g_[___];]:> HoldForm[g],HoldComplete[g_;]:> HoldForm[g]} // Map[ReleaseHold]


Gives the desired symbols - but only when evaluated in a separate kernel, otherwise releasing the hold will replace the symbols by their values:

{selPos,selNeg,selNegPair}

Is there an easier way to accomplish the above?

• What exactly is what you want to do? Do you want to print a list of symbols names or to create an expression/container with those symbols kept unevaluated? – Kuba May 20 '20 at 7:23
• The goal is to build an index or table of which functions/symbols defined in the imported notebook are used in other notebooks - in this case the latter are chapters of a book. The lookups can be done by converting the symbols ToString and importing the target notebooks using NotebookImport[...,"Input"->"InputText"] – alancalvitti May 20 '20 at 17:44
• @Kuba, cont from above, a related application is to take the subset of definitions that appear at least in one of the target notebooks (some definitions might not be used in the project) and automatically generate a new notebook with the definitions sorted by the by some criterion, eg alphabetically by symbol or first chapter in which they appear. Should I ask as a separate Q or edit the above? – alancalvitti May 20 '20 at 18:08

Cases[
exampleInput
, HoldPattern[(Set | SetDelayed)[f_Symbol | f_[___], _]] :> Hold[f]
, {2, 3}
] // Apply[Join]

Hold[selPos, selNeg, selNegPair]


{2,3} is there to handle HoldComplete[ _ ] and HoldComplete[ _ ; ], but I am not sure how flexible it should be because the input cell can contain {{{{{ f =1 }}}}} for example. Then ReplaceAll is a must I guess.
• Thanks, this works except for UpValue definitions. I only added the pattern f_[___][___] to match definitions with SubValues. Assuming that the imported notebook has to be evaluated for this method to be useful, Is it possible to release Hold in the same kernel as without evaluating the definitions? – alancalvitti May 20 '20 at 17:57