2
$\begingroup$

Suppose I have a list:
my_list
where 3 'up's of the list are red(I've made it to Cell Expression as follows).

Cell[BoxData[
RowBox[{"{", 
RowBox[{StyleBox["up", StripOnInput -> False, 
   LineColor -> RGBColor[1, 0, 0], 
   FrontFaceColor -> RGBColor[1, 0, 0], 
   BackFaceColor -> RGBColor[1, 0, 0], 
   GraphicsColor -> RGBColor[1, 0, 0], 
   FontColor -> RGBColor[1, 0, 0]], ",", "right", ",", "up", ",", 
  StyleBox["up", StripOnInput -> False, 
   LineColor -> RGBColor[1, 0, 0], 
   FrontFaceColor -> RGBColor[1, 0, 0], 
   BackFaceColor -> RGBColor[1, 0, 0], 
   GraphicsColor -> RGBColor[1, 0, 0], 
   FontColor -> RGBColor[1, 0, 0]], ",", "down", ",", "down", ",",
   StyleBox["up", StripOnInput -> False, 
   LineColor -> RGBColor[1, 0, 0], 
   FrontFaceColor -> RGBColor[1, 0, 0], 
   BackFaceColor -> RGBColor[1, 0, 0], 
   GraphicsColor -> RGBColor[1, 0, 0], 
   FontColor -> RGBColor[1, 0, 0]], ",", "right", ",", "down", 
  ",", "left"}], "}"}]], "Output", 
CellChangeTimes -> {3.6183594862091923`*^9}]

Then, how can I pick out those elements with style red? Or more, with a specific style?
I tried to use Select[list, Style[#,Red] &] and Cases[list, Style[#,Red] &], but yield nothing.
Thank you!

$\endgroup$

1 Answer 1

7
$\begingroup$

For your specific example. Assigining it to test.

The InputForm:

InputForm[test]

is

{Style[up, LineColor -> RGBColor[1, 0, 0], FrontFaceColor -> RGBColor[1, 0, 0], BackFaceColor -> RGBColor[1, 0, 0], GraphicsColor -> RGBColor[1, 0, 0], FontColor -> RGBColor[1, 0, 0]], right, up, Style[up, LineColor -> RGBColor[1, 0, 0], FrontFaceColor -> RGBColor[1, 0, 0], BackFaceColor -> RGBColor[1, 0, 0], GraphicsColor -> RGBColor[1, 0, 0], FontColor -> RGBColor[1, 0, 0]], down, down, Style[up, LineColor -> RGBColor[1, 0, 0], FrontFaceColor -> RGBColor[1, 0, 0], BackFaceColor -> RGBColor[1, 0, 0], GraphicsColor -> RGBColor[1, 0, 0], FontColor -> RGBColor[1, 0, 0]], right, down, left}

So for this example you can extract the desired elements:

Cases[test, Style[__]]

Or get their positions (which may be an aim) and extract:

pos = Position[test, Style[__]]
Extract[test, pos]
$\endgroup$
2
  • $\begingroup$ I seem to understand. Those 'up's' style are more complex than just a Red, so I have to use BlankSequence? $\endgroup$
    – Ryan
    Aug 30, 2014 at 6:35
  • 2
    $\begingroup$ Nice ... +1 ... could also use Cases[test, _Style]? $\endgroup$
    – kglr
    Aug 30, 2014 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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