Message Boards Message Boards

Make DeleteCases work with string patterns?

I am trying to reproduce the illustration of cleaning up imported data in Mathematica from here.

I had to make a slight change in input 13 to j[x_] := If[StringMatchQ[x, "TOTAL*"], x, DateList[x]]; After running input 14, I get the following output:

enter image description here

There is a "TOTAL" row for every year. The article proposes to delete those rows by running tmp4 = DeleteCases[tmp3, {"TOTAL*", __}]; (the asterisk I my contribution). But it doesn't work! After searching the internet, I found that DeleteCases is not compatible with string patterns. here.

So how do we delete those rows?

And if DeleteCases is truly incompatible, how did the Mathematica article succeed?

PS: I did manage to delete those rows by sorting the data and then Dropping them. But I would still like to know how to proceed with DeleteCases because there might be situations where Sorting doesn't work.

You can combine DeleteCases with StringMatchQ:

DeleteCases[{{"TOTAL 123", 1}},
 {str_ /; StringMatchQ[str, "TOTAL*"], ___}]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract