CONTAINS block for text strings

Thanks! I'm making a drop-down block now, but I can't seem to find the <() contains ()> operator predicate from Scratch.

yeah, about that, it was actually added in scratch 3.0 and snap doesn't have that. Here's how to code it
SnapV script pic (2)

Thanks!

This workaround is even caps sensitive!

lol, that's because the split block is caps sensitive. Do you want it caps sensitive or not?

Um. I would like an option to turn off caps sensitive.

ok. I'll see if there's an easy way to do it.

Ok. Thank you.

here's the edited block, and I think there might be an easier way to do it, but idk.

if you don't want to copy all that code, here's the xml

<blocks app="Snap! 6, https://snap.berkeley.edu" version="1"><block-definition s="%&apos;txt&apos; contains %&apos;value&apos;" type="predicate" category="operators"><header></header><code></code><translations></translations><inputs><input type="%s"></input><input type="%s"></input></inputs><script><block s="doWarp"><script><block s="doDeclareVariables"><list><l>a</l><l>b</l></list></block><block s="doSetVar"><l>value</l><block s="reportTextSplit"><block var="value"/><l><option>letter</option></l></block></block><block s="doSetVar"><l>txt</l><block s="reportTextSplit"><block var="txt"/><l><option>letter</option></l></block></block><block s="doForEach"><l>variable</l><block s="reportNewList"><list><block var="txt"/><block var="value"/></list></block><script><block s="doSetVar"><l>a</l><l>0</l></block><block s="doForEach"><l>item</l><block var="variable"/><script><block s="doChangeVar"><l>a</l><l>1</l></block><block s="doIf"><block s="reportAnd"><block s="reportLessThan"><block s="reportUnicode"><block var="item"/></block><block s="reportUnicode"><l>Z</l></block></block><block s="reportGreaterThan"><block s="reportUnicode"><block var="item"/></block><l>64</l></block></block><script><block s="doReplaceInList"><block var="a"/><block var="variable"/><block s="reportUnicodeAsLetter"><block s="reportSum"><block s="reportUnicode"><block var="item"/></block><l>32</l></block></block></block></script></block></script></block></script></block><block s="doReport"><block s="reportGreaterThan"><block s="reportListLength"><block s="reportTextSplit"><block s="reportJoinWords"><block var="txt"/></block><block s="reportJoinWords"><block var="value"/></block></block></block><l>1</l></block></block></script></block></script></block-definition></blocks>

copy the text and paste it in a text file and save the file as a .xml file. example, "contains block.xml"

Thanks, but what is the join input list block? How do you make it?

Drag txt to the arrows of join. That feature works for any multi-slot input (but not the list reporter); you can drag a list to the arrows and the values of the list are inputted in place of the slots.

Modularity is your friend:


although I might prefer doing this piece of it in JS because (I hope) their toLower function works in non-English character sets.

thanks

It does: untitled script pic (16)

ok, here's a way better way to do it

and here's the xml

<blocks app="Snap! 6, https://snap.berkeley.edu" version="1"><block-definition s="to lowercase %&apos;value&apos;" type="reporter" category="operators"><header></header><code></code><translations></translations><inputs><input type="%s"></input></inputs><script><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list><l>value</l></list><l>return value.toLowerCase()</l></block><list><block var="value"/></list></block></block></script></block-definition><block-definition s="%&apos;txt&apos; contains %&apos;value&apos;" type="predicate" category="operators"><header></header><code></code><translations></translations><inputs><input type="%s"></input><input type="%s"></input></inputs><script><block s="doSetVar"><l>value</l><block s="reportMap"><block s="reifyReporter"><autolambda><custom-block s="to lowercase %s"><l></l></custom-block></autolambda><list></list></block><block s="reportTextSplit"><block var="value"/><l><option>letter</option></l></block></block></block><block s="doSetVar"><l>txt</l><block s="reportMap"><block s="reifyReporter"><autolambda><custom-block s="to lowercase %s"><l></l></custom-block></autolambda><list></list></block><block s="reportTextSplit"><block var="txt"/><l><option>letter</option></l></block></block></block><block s="doReport"><block s="reportGreaterThan"><block s="reportListLength"><block s="reportTextSplit"><block s="reportJoinWords"><block var="txt"/></block><block s="reportJoinWords"><block var="value"/></block></block></block><l>1</l></block></block></script><scripts><script x="96" y="286.83333333333337"><block s="doSetVar"><l>a</l><l>0</l></block><block s="doChangeVar"><l>a</l><l>1</l></block><block s="doIf"><block s="reportAnd"><block s="reportLessThan"><block s="reportUnicode"><block var="item"/></block><block s="reportUnicode"><l>Z</l></block></block><block s="reportGreaterThan"><block s="reportUnicode"><block var="item"/></block><l>64</l></block></block><script><block s="doReplaceInList"><block var="a"/><block var="variable"/><block s="reportUnicodeAsLetter"><block s="reportSum"><block s="reportUnicode"><block var="item"/></block><l>32</l></block></block></block></script></block></script><script x="139" y="211.49999999999994"><block s="doWarp"><script></script></block></script></scripts></block-definition></blocks>

Forum admins, can you split the posts to a new topic, maybe titled "Making of the CONTAINS (a substring) operator", or something similar?

There are several sub-questions going on here, so I can see why you want a split, but I don't see a very clear separation. Ironically, "making the CONTAINS block" is not one of the topics; it's a primitive! The topic you're seeing is "how do you get the INPUT LIST: variant of a block?" to which the answer is to drop a list over the arrowheads of a variadic input. Another piece of it is how to get case-independent searching, to which the answer (not given above, I think) is that CONTAINS uses = for comparisons, which is already case-independent! :~)

If I understand correctly, the CONTAINS primitive that you are referring to is the one from the 'red category' and it searches through items of a list. However this topic seems to be about searching for a substring of a text (not searching for an item of a list). Maybe the title should be "making of a (green, not red) CONTAINS block"?

Yes, you're right. But @ego-lay_atman-bay has solved it.

I've made it even smaller



xml

<blocks app="Snap! 6, https://snap.berkeley.edu" version="1"><block-definition s="%&apos;txt&apos; contains %&apos;value&apos;" type="predicate" category="operators"><header></header><code></code><translations></translations><inputs><input type="%s"></input><input type="%s"></input></inputs><script><block s="doReport"><block s="reportGreaterThan"><block s="reportListLength"><block s="reportTextSplit"><custom-block s="to lowercase %s"><block var="txt"/></custom-block><custom-block s="to lowercase %s"><block var="value"/></custom-block></block></block><l>1</l></block></block></script></block-definition><block-definition s="to lowercase %&apos;value&apos;" type="reporter" category="operators"><header></header><code></code><translations></translations><inputs><input type="%s"></input></inputs><script><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list><l>value</l></list><l>return value.toLowerCase()</l></block><list><block var="value"/></list></block></block></script></block-definition></blocks>