Parallel Arrays

[XML] [Smart Pascal] [JavaScript] [Python] [PHP] [Output]

This example of parallel arrays takes the top six finishers in the Premier League in 2017. There are four parallel arrays, such that for each index from 1 to 6 the club name, points, goals for and goals against correspond. The program outputs all of the details for the team(s) conceding the fewest goals. We used the Blockly Playground to generate the JavaScript, Python and PHP 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="ly;Oz.~v~h$h+SV%m)6^" x="-313" y="-387">
    <field name="VAR">strTeamNames</field>
    <value name="VALUE">
      <block type="text" id="gh]8?,)Ho=mq;dp}bub:">
        <field name="TEXT">Chelsea,Tottenham Hotspur,Manchester City,Liverpool,Arsenal,Manchester United</field>
      </block>
    </value>
    <next>
      <block type="variables_set" id="DIqXCA9|Gm}a[sOsOh`l">
        <field name="VAR">strTeamNamesArray</field>
        <value name="VALUE">
          <block type="lists_split" id="esVh3uy#X|%!mbJ!^_vc">
            <mutation mode="SPLIT"></mutation>
            <field name="MODE">SPLIT</field>
            <value name="INPUT">
              <block type="variables_get" id="!nfx!(pP}~Cw5A6_{rr:">
                <field name="VAR">strTeamNames</field>
              </block>
            </value>
            <value name="DELIM">
              <shadow type="text" id="Oh*x@TGIjJ7t`~kG[|2b">
                <field name="TEXT">,</field>
              </shadow>
            </value>
          </block>
        </value>
        <next>
          <block type="variables_set" id="_.i)`VZ8*0iSDfhE)jIb">
            <field name="VAR">intArrayPoints</field>
            <value name="VALUE">
              <block type="lists_create_with" id="B?-.Nk|bmD1oL`X{ot5,">
                <mutation items="6"></mutation>
                <value name="ADD0">
                  <block type="math_number" id="q.a1L^vQhGphvKF~]^S@">
                    <field name="NUM">93</field>
                  </block>
                </value>
                <value name="ADD1">
                  <block type="math_number" id="T7G!R{$n/p48z~`YpJrq">
                    <field name="NUM">86</field>
                  </block>
                </value>
                <value name="ADD2">
                  <block type="math_number" id="_RZF43o$b3wS37F{^DbV">
                    <field name="NUM">78</field>
                  </block>
                </value>
                <value name="ADD3">
                  <block type="math_number" id="nBT4OD_^y53[Cb$isKq`">
                    <field name="NUM">76</field>
                  </block>
                </value>
                <value name="ADD4">
                  <block type="math_number" id="Xr5[)WY._Hs8G-QFl9~x">
                    <field name="NUM">75</field>
                  </block>
                </value>
                <value name="ADD5">
                  <block type="math_number" id="QNl~+rd_Snw6?=6X6}@2">
                    <field name="NUM">69</field>
                  </block>
                </value>
              </block>
            </value>
            <next>
              <block type="variables_set" id="5J_8_Eh?=[D;WK-+[;kV">
                <field name="VAR">intArrayFor</field>
                <value name="VALUE">
                  <block type="lists_create_with" id="?h`{:SkwXZXV+I$w?]`T">
                    <mutation items="6"></mutation>
                    <value name="ADD0">
                      <block type="math_number" id="BL!YiMCNpWnWP3`8(A|R">
                        <field name="NUM">85</field>
                      </block>
                    </value>
                    <value name="ADD1">
                      <block type="math_number" id="nfsbT_Hg56=cJxf^jjq%">
                        <field name="NUM">86</field>
                      </block>
                    </value>
                    <value name="ADD2">
                      <block type="math_number" id="X6]cnQ2zOIs]K8NAmi00">
                        <field name="NUM">80</field>
                      </block>
                    </value>
                    <value name="ADD3">
                      <block type="math_number" id="D`T{]/E.TU?.=gZF,L@%">
                        <field name="NUM">78</field>
                      </block>
                    </value>
                    <value name="ADD4">
                      <block type="math_number" id="]Y)!x/|5QMLIj$BXGUj.">
                        <field name="NUM">77</field>
                      </block>
                    </value>
                    <value name="ADD5">
                      <block type="math_number" id="NQEJVgZ|3g,1!*Rw(*es">
                        <field name="NUM">54</field>
                      </block>
                    </value>
                  </block>
                </value>
                <next>
                  <block type="variables_set" id="4xve%IIB@{:HSdCmMlhz">
                    <field name="VAR">intArrayAgainst</field>
                    <value name="VALUE">
                      <block type="lists_create_with" id="yg5WK_CT;c4NHfYjzGX`">
                        <mutation items="6"></mutation>
                        <value name="ADD0">
                          <block type="math_number" id="qG(Fs#%*LH%v4YXwJK;!">
                            <field name="NUM">33</field>
                          </block>
                        </value>
                        <value name="ADD1">
                          <block type="math_number" id="RT4{]6Az,t0n/mgUc#N2">
                            <field name="NUM">26</field>
                          </block>
                        </value>
                        <value name="ADD2">
                          <block type="math_number" id="xPk6IEv7QLqG4gIGrh~|">
                            <field name="NUM">39</field>
                          </block>
                        </value>
                        <value name="ADD3">
                          <block type="math_number" id="!xGU3IvR,EL#%p~=k7-z">
                            <field name="NUM">42</field>
                          </block>
                        </value>
                        <value name="ADD4">
                          <block type="math_number" id="/v,N~7*n~)EY{;Cf.qjc">
                            <field name="NUM">44</field>
                          </block>
                        </value>
                        <value name="ADD5">
                          <block type="math_number" id="PHG_3$kalBc=l!)I5kMC">
                            <field name="NUM">29</field>
                          </block>
                        </value>
                      </block>
                    </value>
                    <next>
                      <block type="variables_set" id="#*S_iPtbpUNKU*uIFPiQ">
                        <field name="VAR">intFewestConceded</field>
                        <value name="VALUE">
                          <block type="math_on_list" id="2Lc!_@W|@?za]P]/FrDZ">
                            <mutation op="MIN"></mutation>
                            <field name="OP">MIN</field>
                            <value name="LIST">
                              <block type="variables_get" id="a/4V+8rix%jm-bavVkuw">
                                <field name="VAR">intArrayAgainst</field>
                              </block>
                            </value>
                          </block>
                        </value>
                        <next>
                          <block type="variables_set" id="5[_pnHoo@pd@bOVV*0T)">
                            <field name="VAR">strOutput</field>
                            <value name="VALUE">
                              <block type="text" id="sxZ{gLYAt|pAp;ZFf@wz">
                                <field name="TEXT"></field>
                              </block>
                            </value>
                            <next>
                              <block type="controls_for" id="IJ(1X;ny}.B9eH6H|q4[">
                                <field name="VAR">i</field>
                                <value name="FROM">
                                  <shadow type="math_number" id=",yeS3U,Fs8S;0Qg/+1G8">
                                    <field name="NUM">1</field>
                                  </shadow>
                                </value>
                                <value name="TO">
                                  <shadow type="math_number" id="T8d/HdcL^WE(nG.F#:yN">
                                    <field name="NUM">6</field>
                                  </shadow>
                                </value>
                                <value name="BY">
                                  <shadow type="math_number" id="X)H5@vj=kAp*3|4RSLc5">
                                    <field name="NUM">1</field>
                                  </shadow>
                                </value>
                                <statement name="DO">
                                  <block type="controls_if" id="=yRbWNx_V2F~6.B|Wa[_">
                                    <value name="IF0">
                                      <block type="logic_compare" id="[v0us~n[]Rl+b`n8j:Kr">
                                        <field name="OP">EQ</field>
                                        <value name="A">
                                          <block type="lists_getIndex" id="-;G/%X%g=[0CCTzk;W.Y">
                                            <mutation statement="false" at="true"></mutation>
                                            <field name="MODE">GET</field>
                                            <field name="WHERE">FROM_START</field>
                                            <value name="VALUE">
                                              <block type="variables_get" id="H6~3cFOrRU17zAJ$/h.%">
                                                <field name="VAR">intArrayAgainst</field>
                                              </block>
                                            </value>
                                            <value name="AT">
                                              <block type="variables_get" id="u8d55#%(xJuPv(6OGsv5">
                                                <field name="VAR">i</field>
                                              </block>
                                            </value>
                                          </block>
                                        </value>
                                        <value name="B">
                                          <block type="variables_get" id="m)([[+0~DHZEhs@Dxz~Y">
                                            <field name="VAR">intFewestConceded</field>
                                          </block>
                                        </value>
                                      </block>
                                    </value>
                                    <statement name="DO0">
                                      <block type="text_append" id="$b:q@9n$$ds$;oW]Z0mU">
                                        <field name="VAR">strOutput</field>
                                        <value name="TEXT">
                                          <shadow type="text" id="L;6osAQncY4qgjsn{wb!">
                                            <field name="TEXT"></field>
                                          </shadow>
                                          <block type="lists_getIndex" id="csQ]#!,ZBjl`upz4?#3q">
                                            <mutation statement="false" at="true"></mutation>
                                            <field name="MODE">GET</field>
                                            <field name="WHERE">FROM_START</field>
                                            <value name="VALUE">
                                              <block type="variables_get" id=";/9dT;*lz:MA`2%1$^4T">
                                                <field name="VAR">strTeamNamesArray</field>
                                              </block>
                                            </value>
                                            <value name="AT">
                                              <block type="variables_get" id="Si!CX9aC6tB.C^Q[K,Yp">
                                                <field name="VAR">i</field>
                                              </block>
                                            </value>
                                          </block>
                                        </value>
                                        <next>
                                          <block type="text_append" id="+si7wPe#w.rozS?W5Mv9">
                                            <field name="VAR">strOutput</field>
                                            <value name="TEXT">
                                              <shadow type="text" id="OtiMjK,tirW;T:iGjj%B">
                                                <field name="TEXT"> conceded the fewest goals  </field>
                                              </shadow>
                                              <block type="text_join" id="L)Vh.R?}T_Fo8l[I-iV$">
                                                <mutation items="9"></mutation>
                                                <value name="ADD0">
                                                  <block type="text" id="1QiV3*#2?bG=irtZvDDi">
                                                    <field name="TEXT"> in position </field>
                                                  </block>
                                                </value>
                                                <value name="ADD1">
                                                  <block type="variables_get" id="fTb2+em+WlhS,NbWh,wc">
                                                    <field name="VAR">i</field>
                                                  </block>
                                                </value>
                                                <value name="ADD2">
                                                  <block type="text" id="C-6gGPU*Im.z*hNSuP/5">
                                                    <field name="TEXT">   conceded the fewest goals  (</field>
                                                  </block>
                                                </value>
                                                <value name="ADD3">
                                                  <block type="variables_get" id="Oet6|%c4HYkNeM^xXRD:">
                                                    <field name="VAR">intFewestConceded</field>
                                                  </block>
                                                </value>
                                                <value name="ADD4">
                                                  <block type="text" id="[w(JfjvtnCnBgL{=zJ7~">
                                                    <field name="TEXT">). They scored </field>
                                                  </block>
                                                </value>
                                                <value name="ADD5">
                                                  <block type="lists_getIndex" id="!u*Sy0ydY^k@+BBeQt/?">
                                                    <mutation statement="false" at="true"></mutation>
                                                    <field name="MODE">GET</field>
                                                    <field name="WHERE">FROM_START</field>
                                                    <value name="VALUE">
                                                      <block type="variables_get" id=";2k.dZR#vYBG.?ehfCwg">
                                                        <field name="VAR">intArrayFor</field>
                                                      </block>
                                                    </value>
                                                    <value name="AT">
                                                      <block type="variables_get" id="IKTjNVA9$F/+t5#0CWu(">
                                                        <field name="VAR">i</field>
                                                      </block>
                                                    </value>
                                                  </block>
                                                </value>
                                                <value name="ADD6">
                                                  <block type="text" id=")@+%RD/1,O?-8W3Om6~Q">
                                                    <field name="TEXT"> goals and obtained </field>
                                                  </block>
                                                </value>
                                                <value name="ADD7">
                                                  <block type="lists_getIndex" id="`!2IqZAP9D_AwJNtbu#D">
                                                    <mutation statement="false" at="true"></mutation>
                                                    <field name="MODE">GET</field>
                                                    <field name="WHERE">FROM_START</field>
                                                    <value name="VALUE">
                                                      <block type="variables_get" id="4[)X1*c72g{^CkkJyIRi">
                                                        <field name="VAR">intArrayPoints</field>
                                                      </block>
                                                    </value>
                                                    <value name="AT">
                                                      <block type="variables_get" id="2fcPOjd?]YY18(`6]LLf">
                                                        <field name="VAR">i</field>
                                                      </block>
                                                    </value>
                                                  </block>
                                                </value>
                                                <value name="ADD8">
                                                  <block type="text" id="k^a^$2(m2QZjhc+5|oL8">
                                                    <field name="TEXT"> points.</field>
                                                  </block>
                                                </value>
                                              </block>
                                            </value>
                                          </block>
                                        </next>
                                      </block>
                                    </statement>
                                  </block>
                                </statement>
                                <next>
                                  <block type="text_print" id="eW+g~.RHFwms+B~w],R.">
                                    <value name="TEXT">
                                      <shadow type="text" id="G4(@_T-uTdU_HUZDXsry">
                                        <field name="TEXT">abc</field>
                                      </shadow>
                                      <block type="variables_get" id="[Os`Ep|+zYA1Py~(k`ON">
                                        <field name="VAR">strOutput</field>
                                      </block>
                                    </value>
                                  </block>
                                </next>
                              </block>
                            </next>
                          </block>
                        </next>
                      </block>
                    </next>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var intArrayAgainst: TIntArray;
var intArrayFor: TIntArray;
var intArrayPoints: TIntArray;
var intFewestConceded: Integer;
var strOutput: String;
var strTeamNames: String;
var strTeamNamesArray: TStrArray;


function MinValueFloat(Arr: TFloatArray): Float;
begin
  Result := Arr[0];
  for var i := 1 to Arr.Length - 1 do
    if Arr[i] < Result then
    Result := Arr[i];
end;


function MinValueInt(Arr: TIntArray): Integer;
begin
  Result := Arr[0];
  for var i := 1 to Arr.Length - 1 do
    if Arr[i] < Result then
    Result := Arr[i];
end;


function Str(v: Variant) : string;
begin
  asm
    if (@v === 0) {
      @Result = '0';
    }
    else if (@v === '') {
      @Result = '';
    }
    else if (isNaN(@v)) {
      @Result = @v;
    }
    else {
      @Result = (@v).toString();
    }
  end;
end;



strTeamNames := 'Chelsea,Tottenham Hotspur,Manchester City,Liverpool,Arsenal,Manchester United';
strTeamNamesArray := StrSplit(strTeamNames, ',');
intArrayPoints := [93, 86, 78, 76, 75, 69];
intArrayFor := [85, 86, 80, 78, 77, 54];
intArrayAgainst := [33, 26, 39, 42, 44, 29];
intFewestConceded := MinValueInt(intArrayAgainst);
strOutput := '';
for var i := 1 to 6 do
begin
  if ((intArrayAgainst[(i - 1)] = intFewestConceded)) then
  begin
    strOutput += strTeamNamesArray[(i - 1)];
    strOutput += StrJoin([Str(' in position '), Str(i), Str('   conceded the fewest goals  ('), Str(intFewestConceded), Str('). They scored '), Str(intArrayFor[(i - 1)]), Str(' goals and obtained '), Str(intArrayPoints[(i - 1)]), Str(' points.')],'');
  end;
end;
Console.writeln(strOutput);
    

Generated JavaScript Code

var strOutput, i, intArrayPoints, intArrayFor, intFewestConceded, strTeamNamesArray, intArrayAgainst, strTeamNames;


strTeamNames = 'Chelsea,Tottenham Hotspur,Manchester City,Liverpool,Arsenal,Manchester United';
strTeamNamesArray = strTeamNames.split(',');
intArrayPoints = [93, 86, 78, 76, 75, 69];
intArrayFor = [85, 86, 80, 78, 77, 54];
intArrayAgainst = [33, 26, 39, 42, 44, 29];
intFewestConceded = Math.min.apply(null, intArrayAgainst);
strOutput = '';
for (i = 1; i <= 6; i++) {
  if (intArrayAgainst[(i - 1)] == intFewestConceded) {
    strOutput = String(strOutput) + String(strTeamNamesArray[(i - 1)]);
    strOutput = String(strOutput) + String([' in position ',i,'   conceded the fewest goals  (',intFewestConceded,'). They scored ',intArrayFor[(i - 1)],' goals and obtained ',intArrayPoints[(i - 1)],' points.'].join(''));
  }
}
window.alert(strOutput);

Generated Python Code

strOutput = None
i = None
intArrayPoints = None
intArrayFor = None
intFewestConceded = None
strTeamNamesArray = None
intArrayAgainst = None
strTeamNames = None


strTeamNames = 'Chelsea,Tottenham Hotspur,Manchester City,Liverpool,Arsenal,Manchester United'
strTeamNamesArray = strTeamNames.split(',')
intArrayPoints = [93, 86, 78, 76, 75, 69]
intArrayFor = [85, 86, 80, 78, 77, 54]
intArrayAgainst = [33, 26, 39, 42, 44, 29]
intFewestConceded = min(intArrayAgainst)
strOutput = ''
for i in range(1, 7):
  if intArrayAgainst[int(i - 1)] == intFewestConceded:
    strOutput = str(strOutput) + str(strTeamNamesArray[int(i - 1)])
    strOutput = str(strOutput) + str(''.join([str(x) for x in [' in position ', i, '   conceded the fewest goals  (', intFewestConceded, '). They scored ', intArrayFor[int(i - 1)], ' goals and obtained ', intArrayPoints[int(i - 1)], ' points.']]))
print(strOutput)

Generated PHP Code

$strOutput;
$i;
$intArrayPoints;
$intArrayFor;
$intFewestConceded;
$strTeamNamesArray;
$intArrayAgainst;
$strTeamNames;


$strTeamNames = 'Chelsea,Tottenham Hotspur,Manchester City,Liverpool,Arsenal,Manchester United';
$strTeamNamesArray = explode(',', $strTeamNames);
$intArrayPoints = array(93, 86, 78, 76, 75, 69);
$intArrayFor = array(85, 86, 80, 78, 77, 54);
$intArrayAgainst = array(33, 26, 39, 42, 44, 29);
$intFewestConceded = min($intArrayAgainst);
$strOutput = '';
for ($i = 1; $i <= 6; $i++) {
  if ($intArrayAgainst[($i - 1)] == $intFewestConceded) {
    $strOutput .= $strTeamNamesArray[($i - 1)];
    $strOutput .= implode('', array(' in position ',$i,'   conceded the fewest goals  (',$intFewestConceded,'). They scored ',$intArrayFor[($i - 1)],' goals and obtained ',$intArrayPoints[($i - 1)],' points.'));
  }
}
print($strOutput);

Copy of Output

Tottenham Hotspur in position 2 conceded the fewest goals (26).
They scored 86 goals and obtained 86 points.
Programming - a skill for life!

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