MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Select and Cases Give Different Answers

Gregory Lypny wrote:
> Hello everyone,
> Suppose I have the following list that is a mixture of the string "NA" 
> and five numeric quantities.
> 	theList = {"NA", "NA", -0.01315, 0.0120957, -1/41, 0.00625, "NA", 
> "NA", 5/8,  "NA"}
> I want to weed out the NAs, and all of the following
> 	Cases[theList, _?NumericQ]
> 	DeleteCases[theList, _?StringQ]
> 	Select[theList, # =E2=88=88 Reals &]
> 	Select[theList, # =!= "NA" &]
> work fine because each returns {-0.01315, 0.0120957, -(1/41), 0.00625, 
> 5/8}.
> But I don't understand why Unequal, when used as the criterion for 
> Select as in
> 	Select[theList, # != "NA" &],
> fails to return the rationals, and I get only three elements 
> {-0.01315, 0.0120957, 0.00625}.  I'd appreciate any clarification on 
> the difference between UnsameQ and Unequal because my inclination in 
> most circumstances is to use an equal sign or an unequal sign to make 
> simple comparisons yet it doesn't give me the answer I expect, and I'd 
> hate to make a costly mistake somewhere down the road!
> Regards,
> 	Gregory
Remember that if Mathematica can't evaluate something, it just returns 
it unchanged. Unequal only returns True/False if it can do the 
calculation - i.e. if both sides are numbers, or both are strings, or a 
few obvious cases like xxx=xxx - otherwise it returns unevaluated. One 
might have thought it would 'know' a string would never equal a 
rational, but it would seem it doesn't! Select only picks those elements 
for which the test returns True.

UnsameQ actually compares its arguments to determine if they have the 
same structure - i.e. it always returns True or False.

David Bailey

  • Prev by Date: Re: Mathematica crashed -- where did my notebook go?
  • Next by Date: Re: Mathematica crashed -- where did my notebook go?
  • Previous by thread: Re: Select and Cases Give Different Answers
  • Next by thread: Hadamard von Koch