Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
up vote 7 down vote accepted

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]
share|improve this answer
    
I seem to understand. Those 'up's' style are more complex than just a Red, so I have to use BlankSequence? – nix Aug 30 '14 at 6:35
2  
Nice ... +1 ... could also use Cases[test, _Style]? – kglr Aug 30 '14 at 15:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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