Searching an array

[XML] [Smart Pascal] [JavaScript] [Python] [output] [Test of Continue]

We demonstrate how to perform a linear search of an array, using male Wimbledon singles multi-champions as an example. We print each element checked to confirm that the block for breaking out of a loop works.

We used the Blockly Playground to generate JavaScript and Python code from the same blocks.
The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="variables_set" id="QSa`lg$@=7~sNEqqk93#" x="13" y="13">
    <field name="VAR">BoolFound</field>
    <value name="VALUE">
      <block type="logic_boolean" id="zdW_KT;P+Q?385/!N_2u">
        <field name="BOOL">FALSE</field>
      </block>
    </value>
    <next>
      <block type="variables_set" id="`qzOzq16-KxfL$26Bi.X">
        <field name="VAR">strChamps</field>
        <value name="VALUE">
          <block type="text" id="o=YMIBu:=q0q[qS%2k*#">
            <field name="TEXT">Laver,Newcombe,Connors,Borg,McEnroe,Becker,Edberg,Sampras,Federer,Nadal,Murray</field>
          </block>
        </value>
        <next>
          <block type="variables_set" id="2@p6;xRJ-m9OdgID}~R+">
            <field name="VAR">strChampsArray</field>
            <value name="VALUE">
              <block type="lists_split" id="Axc.d~O6qLco,zp}{L`d">
                <mutation mode="SPLIT"></mutation>
                <field name="MODE">SPLIT</field>
                <value name="INPUT">
                  <block type="variables_get" id="[W)a^AOP8!f$-!~#ijC2">
                    <field name="VAR">strChamps</field>
                  </block>
                </value>
                <value name="DELIM">
                  <shadow type="text" id="7]#+rf1R[J%,9372IpF@">
                    <field name="TEXT">,</field>
                  </shadow>
                </value>
              </block>
            </value>
            <next>
              <block type="variables_set" id="9kOni1u+d%(+AYCByCT]">
                <field name="VAR">strTarget</field>
                <value name="VALUE">
                  <block type="text_prompt_ext" id="-63t=6[JB2X?ngjQz--#">
                    <mutation type="TEXT"></mutation>
                    <field name="TYPE">TEXT</field>
                    <value name="TEXT">
                      <shadow type="text" id="O[#O=Yls:kEIW!S`$U2g">
                        <field name="TEXT">Please enter the surname of a Wimbledon singles multi-champion</field>
                      </shadow>
                    </value>
                  </block>
                </value>
                <next>
                  <block type="controls_forEach" id=";]y@IJ^:mFI+%IYM^4W7">
                    <field name="VAR">i</field>
                    <value name="LIST">
                      <block type="variables_get" id="or(x{krvl}8x`f#*,D5#">
                        <field name="VAR">strChampsArray</field>
                      </block>
                    </value>
                    <statement name="DO">
                      <block type="text_print" id="#-*MH#ljVJPRbUvp)LLm">
                        <value name="TEXT">
                          <shadow id="y9#XToYFTpaZHv/7vqY~" type="text">
                            <field name="TEXT">abc</field>
                          </shadow>
                          <block type="variables_get" id="J,b%-vg8G`-/-_^((~8p">
                            <field name="VAR">i</field>
                          </block>
                        </value>
                        <next>
                          <block type="controls_if" id="O9xy2xz!g{{;$GraUu~x">
                            <value name="IF0">
                              <block type="logic_compare" id="+-6iQ|[gX!QS^UW#B6.I">
                                <field name="OP">EQ</field>
                                <value name="A">
                                  <block type="text_changeCase" id="d/Cq2j/_`_wy)7eKK4do">
                                    <field name="CASE">UPPERCASE</field>
                                    <value name="TEXT">
                                      <shadow id="OAT|yz4UmlV(%#9I]Ecv" type="text">
                                        <field name="TEXT">abc</field>
                                      </shadow>
                                      <block type="variables_get" id="g;mr.0Erj.[q~MYFW?ll">
                                        <field name="VAR">i</field>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                                <value name="B">
                                  <block type="text_changeCase" id="cgSTgl:~_m[#yCd,9vVU">
                                    <field name="CASE">UPPERCASE</field>
                                    <value name="TEXT">
                                      <shadow id="OAT|yz4UmlV(%#9I]Ecv" type="text">
                                        <field name="TEXT">abc</field>
                                      </shadow>
                                      <block type="variables_get" id=";I=W]30e#g}{Qs@*`Ob/">
                                        <field name="VAR">strTarget</field>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <statement name="DO0">
                              <block type="variables_set" id="ElswC+v^)gY(.ev6.xp?">
                                <field name="VAR">BoolFound</field>
                                <value name="VALUE">
                                  <block type="logic_boolean" id="DXi/qAom]v2P$-$~Kh3!">
                                    <field name="BOOL">TRUE</field>
                                  </block>
                                </value>
                                <next>
                                  <block type="variables_set" id="),{efRs-})i$v;gnt[($">
                                    <field name="VAR">strMessage</field>
                                    <value name="VALUE">
                                      <block type="variables_get" id="f_I9zmaNg^iqisTB?o/0">
                                        <field name="VAR">i</field>
                                      </block>
                                    </value>
                                    <next>
                                      <block type="text_append" id="*tn6}xeNo1E/;bM@2tCm">
                                        <field name="VAR">strMessage</field>
                                        <value name="TEXT">
                                          <shadow type="text" id="9b1sy8wIA#DUvJwxbFoJ">
                                            <field name="TEXT"> found.</field>
                                          </shadow>
                                        </value>
                                        <next>
                                          <block type="controls_flow_statements" id="Q#WjVm^,GQ{G0!i@:k$^">
                                            <field name="FLOW">BREAK</field>
                                          </block>
                                        </next>
                                      </block>
                                    </next>
                                  </block>
                                </next>
                              </block>
                            </statement>
                          </block>
                        </next>
                      </block>
                    </statement>
                    <next>
                      <block type="controls_if" id="81eRfTnMQtEEe^P_i)xd">
                        <value name="IF0">
                          <block type="logic_compare" id="@NIA.5MX}7Y(qrVIJ/#v">
                            <field name="OP">EQ</field>
                            <value name="A">
                              <block type="variables_get" id="Daaa5n#?2jNJ24)j#KfL">
                                <field name="VAR">BoolFound</field>
                              </block>
                            </value>
                            <value name="B">
                              <block type="logic_boolean" id="Z};JGR2S$g$o`|Y;J8((">
                                <field name="BOOL">FALSE</field>
                              </block>
                            </value>
                          </block>
                        </value>
                        <statement name="DO0">
                          <block type="variables_set" id="_RosB$N-b3uwY;E.v`;=">
                            <field name="VAR">strMessage</field>
                            <value name="VALUE">
                              <block type="variables_get" id="5=|Q[vYm)3AR+HF$J|Ve">
                                <field name="VAR">strTarget</field>
                              </block>
                            </value>
                            <next>
                              <block type="text_append" id="T[D^ssFFTY)U}fX?)d,V">
                                <field name="VAR">strMessage</field>
                                <value name="TEXT">
                                  <shadow type="text" id="}-5Oj=N%f19iu0sD%q?0">
                                    <field name="TEXT"> not found.</field>
                                  </shadow>
                                </value>
                              </block>
                            </next>
                          </block>
                        </statement>
                        <next>
                          <block type="text_print" id=";w@[U=8,_i3QFl,@BlG0">
                            <value name="TEXT">
                              <shadow id="y9#XToYFTpaZHv/7vqY~" type="text">
                                <field name="TEXT">abc</field>
                              </shadow>
                              <block type="variables_get" id="Sl!]35L|O)6x}Vy#C/tH">
                                <field name="VAR">strMessage</field>
                              </block>
                            </value>
                          </block>
                        </next>
                      </block>
                    </next>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var strMessage: String;
var strTarget: String;
var BoolFound: Boolean;
var strChampsArray: TStrArray;
var strChamps: String;



BoolFound := false;
strChamps := 'Laver,Newcombe,Connors,Borg,McEnroe,Becker,Edberg,Sampras,Federer,Nadal,Murray';
strChampsArray := StrSplit(strChamps, ',');
strTarget := prompt('Please enter the surname of a Wimbledon singles multi-champion');
for var i in strChampsArray do
begin
  Console.writeln(i);
  if ((UpperCase(i) = UpperCase(strTarget))) then
  begin
    BoolFound := true;
    strMessage := i;
    strMessage += ' found.';
    break;
  end;
end;
if ((BoolFound = false)) then
begin
  strMessage := strTarget;
  strMessage += ' not found.';
end;
Console.writeln(strMessage);
    

Generated JavaScript

var strMessage, strTarget, BoolFound, i, strChampsArray, strChamps;


BoolFound = false;
strChamps = 'Laver,Newcombe,Connors,Borg,McEnroe,Becker,Edberg,Sampras,Federer,Nadal,Murray';
strChampsArray = strChamps.split(',');
strTarget = window.prompt('Please enter the surname of a Wimbledon singles multi-champion');
for (var i_index in strChampsArray) {
  i = strChampsArray[i_index];
  window.alert(i);
  if (i.toUpperCase() == strTarget.toUpperCase()) {
    BoolFound = true;
    strMessage = i;
    strMessage = String(strMessage) + String(' found.');
    break;
  }
}
if (BoolFound == false) {
  strMessage = strTarget;
  strMessage = String(strMessage) + String(' not found.');
}
window.alert(strMessage);

Generated Python Code

strMessage = None
strTarget = None
BoolFound = None
i = None
strChampsArray = None
strChamps = None

def text_prompt(msg):
  try:
    return raw_input(msg)
  except NameError:
    return input(msg)


BoolFound = False
strChamps = 'Laver,Newcombe,Connors,Borg,McEnroe,Becker,Edberg,Sampras,Federer,Nadal,Murray'
strChampsArray = strChamps.split(',')
strTarget = text_prompt('Please enter the surname of a Wimbledon singles multi-champion')
for i in strChampsArray:
  print(i)
  if i.upper() == strTarget.upper():
    BoolFound = True
    strMessage = i
    strMessage = str(strMessage) + str(' found.')
    break
if BoolFound == False:
  strMessage = strTarget
  strMessage = str(strMessage) + str(' not found.')
print(strMessage)

Copy of output from two runs of the generated Smart Pascal program

Laver
Newcombe
Connors
Borg
McEnroe
Becker
Edberg
Sampras
Federer
Nadal
Murray
Morrison not found.

Laver
Newcombe
Connors
Connors found.

Testing of Continue to the next Iteration

The above code tested the breaking out of a loop. The same block can also be used to exit from the processing of the current iteration of the loop and to proceed with the next iteration (if the current iteration is not the last). We inserted an if statement as shown in this screenshot.

Inserted blocks

Inserted blocks

The additional code (XML of the new blocks and Smart Pascal if statement) follow.
<block type="controls_if" id="bb+Ej=1mt(s(z[Dy,=(M">
  <value name="IF0">
    <block type="logic_compare" id="M`OSu,/1FonqxP_p4Z;|">
      <field name="OP">NEQ</field>
      <value name="A">
        <block type="text_changeCase" id="2|!rCkI|kW8vKXp|k=(u">
          <field name="CASE">UPPERCASE</field>
          <value name="TEXT">
            <shadow type="text" id="OAT|yz4UmlV(%#9I]Ecv">
              <field name="TEXT">abc</field>
            </shadow>
            <block type="text_charAt" id="zedy]%T`]vt0}[{A1(O!">
              <mutation at="false"></mutation>
              <field name="WHERE">FIRST</field>
              <value name="VALUE">
                <block type="variables_get" id="981;b:``FL*7}m[cjDTl">
                  <field name="VAR">i</field>
                </block>
              </value>
            </block>
          </value>
        </block>
      </value>
      <value name="B">
        <block type="text_changeCase" id="bqQ|WlmzxehtMLYB4??9">
          <field name="CASE">UPPERCASE</field>
          <value name="TEXT">
            <shadow id="OAT|yz4UmlV(%#9I]Ecv" type="text">
              <field name="TEXT">abc</field>
            </shadow>
            <block type="text_charAt" id="/M{0oi#RkMj=A|jsu!sR">
              <mutation at="false"></mutation>
              <field name="WHERE">FIRST</field>
              <value name="VALUE">
                <block type="variables_get" id="sbo(nSis6]%5)!6/K)DV">
                  <field name="VAR">strTarget</field>
                </block>
              </value>
            </block>
          </value>
        </block>
      </value>
    </block>
  </value>
  <statement name="DO0">
    <block type="text_print" id="r^`oCG(L,L`c(z_=TnvL">
      <value name="TEXT">
        <shadow type="text" id="5Ol*}8v}H^0r;7i:f?`B">
          <field name="TEXT">First letter not matched</field>
        </shadow>
      </value>
      <next>
        <block type="controls_flow_statements" id="blHQJ~caUjd8`)HXv3lu">
          <field name="FLOW">CONTINUE</field>
        </block>
      </next>
    </block>
  </statement>
  <next>

  if (UpperCase(LeftStr(i, 1)) <> UpperCase(LeftStr(strTarget, 1))) then
  begin
    Console.writeln('First letter not matched');
    continue;
  end;    
An example of the output is:
First letter not matched
First letter not matched
Connors
Connors found.
Programming - a skill for life!

XML, images and generated Smart Pascal code of examples including loops, arrays, validation and recursion