5
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ Related: (3916), (9269), (9702), (44256) $\endgroup$ Commented Apr 30, 2014 at 8:11
  • $\begingroup$ GroupBy[animals, Last][100] $\endgroup$ Commented Aug 30, 2023 at 11:32

1 Answer 1

11
$\begingroup$
animals={{dog,black,200},{cat,white,100},{moose,brown,500},{zebra,black,100}} Cases[animals,{_,_,100}] (*or*) Select[animals,#[[3]]==100&] 

Among others...

$\endgroup$
5
  • $\begingroup$ 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! $\endgroup$ Commented Apr 6, 2014 at 1:39
  • $\begingroup$ @DavidKerr: Have a look at q/a here and here. Both excellent mini-tutorials at this very site. $\endgroup$ Commented Apr 6, 2014 at 1:52
  • $\begingroup$ 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? $\endgroup$ Commented Apr 6, 2014 at 2:02
  • $\begingroup$ 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. $\endgroup$ Commented Apr 6, 2014 at 5:09
  • $\begingroup$ Thanks. If you get pointed in the right direction with a few hints it gives geometric progress! $\endgroup$ Commented Apr 6, 2014 at 13:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.