Message Boards Message Boards

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

Pattern with alternatives

Posted 11 years ago
Hi everyone,

How we input the "|" in Mathematica for alternative pattern matching like the following code:
Cases[Range[1, 100], x_/;Mod[x,2]==0 | Mod[x,3]==0]

Thanks in advance!

N.B: Sorry, I feel stupid! I found my error!

Have a nice weekend!
POSTED BY: Steeve Brechmann
2 Replies
What you may be looking for there is logical Or for that pair of equalities, double vertical pipe, ||
In[ ]:= Cases[Range[1, 100], x_ /; (Mod[x, 2] == 0 || Mod[x, 3] == 0)]

Out[ ]= {2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, \
26, 27, 28, 30, 32, 33, 34, 36, 38, 39, 40, 42, 44, 45, 46, 48, 50, \
51, 52, 54, 56, 57, 58, 60, 62, 63, 64, 66, 68, 69, 70, 72, 74, 75, \
76, 78, 80, 81, 82, 84, 86, 87, 88, 90, 92, 93, 94, 96, 98, 99, 100}
Cases[Range[1, 100], x_ /; Or[Mod[x, 2] == 0, Mod[x, 3] == 0]]
Cases[Range[1, 100], x_ /; (Mod[x, 2] == 0 || Mod[x, 3] == 0)]
POSTED BY: Nasser M. Abbasi
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