Message Boards Message Boards

1
|
4791 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

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
POSTED BY: Josh Lietz
2 Replies
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 BY: Buddy Ritchie
Table[MatchQ[i, {{_, Except[{0 ..}, {__?NumericQ}]}}], {i, {good, bad1, bad2}}]
POSTED BY: Seth Chandler
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