I have an array of triplets that can be approximated by the following code:
data = Table[{RandomInteger[5], RandomInteger[10], RandomChoice[{"ABC", "BCD", "DEF"}]}, {10}]
The first two elements of the triplet are data and the third element is a set of single-character flags. Let's say that the flag "D" means "don't use" so I want to delete any cases containing a "D" flag. Easy enough:
DeleteCases[data, x_ /; StringCases[x[[3]], "D"] == {"D"}]
The problem comes in for a case where a triplet contains no flags. In this case, the third element is Null
:
data = Table[{RandomInteger[5], RandomInteger[10], RandomChoice[{"ABC", "BCD", "DEF", Null}]}, {10}]
DeleteCases[data, x_ /; StringCases[x[[3]], "D"] == {"D"}]
The StringCases
function throws an error whenever it attempts to compare Null
to "D". I would like to avoid this error message without using something like Quiet
just in case there are other errors that shouldn't be hidden.
So the question is: how does one formulate a substring search that returns False
instead of throwing an error if the variable passed does not have a Head
of String
?