3
$\begingroup$

Somehow, I did not manage to find anything related to that. Suppose, in my Global list of variables, I would like to make a search such that I obtain all the elements in the list which start with func. Trying the naive choice of

Cases[Names["Global`*"], func_]

does not work, however I cannot see how to make this work?

$\endgroup$
5
$\begingroup$

You can use PatternTest (?) with StringMatchQ

Cases[Names["Global`*"], _?(StringMatchQ["func*"])]

or with StringStartsQ

Cases[Names["Global`*"], _?(StringStartsQ["func"]) ]

Note the redundant-looking (..) after ?.

Alternatively, you can use Condition (/;)

Cases[Names["Global`*"], x_ /; StringMatchQ[ x, "func*"] ]
Cases[Names["Global`*"], x_/;StringStartsQ[ x, "func"] ]
$\endgroup$
5
$\begingroup$

Or, if you are not wedded to Cases and patterns, you could use Select and a predicate, something like

Select[Names["Global`*"], StringStartsQ["func"]]
$\endgroup$
4
$\begingroup$

Why not simply include the pattern in Names?

In[1]:= Names["System`Plot*"]

Out[1]= {"Plot", "Plot3D", "Plot3Matrix", "PlotDivision", \
"PlotJoined", "PlotLabel", "PlotLabels", "PlotLayout", "PlotLegends", \
"PlotMarkers", "PlotPoints", "PlotRange", "PlotRangeClipping", \
"PlotRangeClipPlanesStyle", "PlotRangePadding", "PlotRegion", \
"PlotStyle", "PlotTheme"}
$\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.