Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

1 Answer 1

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 at 6:35
2  
Nice ... +1 ... could also use Cases[test, _Style]? –  kguler Aug 30 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.