1
|
4923 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
GROUPS:

# Repeated[Except[foo, bar]] in pattern

Posted 12 years ago
 Hi, Given this data: good = {{{"foo"}, {0, 1, 0, 0}}};bad1 = {{{"foo"}, {0, 0, 0, 0}}};bad2 = {{{"foo"}, {0, "foo", 0, 0}}};I want the test to evaluate to True if the numeric list contains only numbers, but not all zeros. This gets me halfway there by catching the bad string: In[151]:= Table[If[MatchQ[i, {{_, {_?NumberQ ..}} ..}], "pass", "fail"],{i, {good, bad1, bad2}}]Out[151]= {"pass", "pass", "fail"}but adding Except to catch the repeated 0 cases doesn't seem to work like I thought it might: In[154]:= Table[If[MatchQ[i, {{_, {Except[0, _?NumberQ] ..}} ..}],"pass", "fail"], {i, {good, bad1, bad2}}]Out[154]= {"fail", "fail", "fail"}I've played around with some other patterns, but haven't got it just right. Other approaches?Thanks,Josh
2 Replies
Sort By:
Posted 12 years ago
 Another way to skin the cat... In[62]:=good = {{{"foo"}, {0, 1, 0, 0}}};bad1 = {{{"foo"}, {0, 0, 0, 0}}};bad2 = {{{"foo"}, {0, "foo", 0, 0}}};In[79]:=testQ = MatchQ[#, {__?NumberQ}] && ! MatchQ[#, {0 ..}] &;testQ /@ (Last /@ Last /@ {good, bad1, bad2})Out[80]= {True, False, False}
Posted 12 years ago