5

I have the following:

animals = {{dog, black, 200}, {cat, white, 100}, {moose, brown, 500}, {zebra,black,100}}

If this came from a SQL table called animals, which has fields named: type of animal, color, weight

I would say:

Select * from animals WHERE weight = 100

How do you do this in Mathematica?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
David Kerr
  • 583
  • 1
  • 4
  • 11

1 Answers1

11
animals={{dog,black,200},{cat,white,100},{moose,brown,500},{zebra,black,100}}

Cases[animals,{_,_,100}]

(*or*)

Select[animals,#[[3]]==100&]

Among others...

ciao
  • 25,774
  • 2
  • 58
  • 139
  • Thanks!... I am new to Mathematica and I am finding getting up to speed despite reading 5 books on it is hard. Appreciate the help! – David Kerr Apr 06 '14 at 01:39
  • @DavidKerr: Have a look at q/a here and here. Both excellent mini-tutorials at this very site. – ciao Apr 06 '14 at 01:52
  • Thanks for the links. So for say greater than 100 how would you do that in the Cases command? I figured out how to do in select. What does the # and & do in cases? – David Kerr Apr 06 '14 at 02:02
  • Cases[animals,{_,_,x_/;x>100}]. The x gives the placeholder a name you can then do things with (like test). The # and & in Select are placeholder (slot) and pure-function elements. Check the docs for Select, it covers it there. – ciao Apr 06 '14 at 05:09
  • Thanks. If you get pointed in the right direction with a few hints it gives geometric progress! – David Kerr Apr 06 '14 at 13:33