Subarrays, combining arrays, array sorting and converting arrays to text

[XML] [Smart Pascal] [Output]

This example demonstrates how to copy a sub-array from an array and how to sort an array. (Make sure you copy all of it if you might need the unsorted array again!).

Note the use of the hard breaking space (&nbsp;) and line break <br> within the text to format it for the HTML output from the Smart Mobile Studio console application.

We only use the ascending sort of integers here but you can see the other comparison functions that have been generated for use if necessary.

See the following pages for an easy way to combine arrays and an example of converting an array to text.

The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="variables_set" id=")aKcp8vb.EX?Gq[dTL]+" x="-845" y="-857">
    <field name="VAR">intRainfallArray</field>
    <value name="VALUE">
      <block type="lists_create_with" id="7j^Eyof!ijWgbt[bdiq+">
        <mutation items="12"></mutation>
        <value name="ADD0">
          <block type="math_number" id="{M:z(m+njeaQeXZ1V5gs">
            <field name="NUM">52</field>
          </block>
        </value>
        <value name="ADD1">
          <block type="math_number" id="#z./^Y$HP(Rk?f4b1k6I">
            <field name="NUM">39</field>
          </block>
        </value>
        <value name="ADD2">
          <block type="math_number" id="@xUXAhm7@:`eg]l-m:/f">
            <field name="NUM">35</field>
          </block>
        </value>
        <value name="ADD3">
          <block type="math_number" id="btlG*Afh2D]~5JIPMvaZ">
            <field name="NUM">43</field>
          </block>
        </value>
        <value name="ADD4">
          <block type="math_number" id="Eq^l@4%@56`u%yx-cp`2">
            <field name="NUM">50</field>
          </block>
        </value>
        <value name="ADD5">
          <block type="math_number" id="Yc~h`i`)TF_.Cq!Jc^)I">
            <field name="NUM">43</field>
          </block>
        </value>
        <value name="ADD6">
          <block type="math_number" id="fxt1,y=j(+64nY;~Ksnk">
            <field name="NUM">41</field>
          </block>
        </value>
        <value name="ADD7">
          <block type="math_number" id="K6%o$-(Zg+9gt:ZeF=T!">
            <field name="NUM">48</field>
          </block>
        </value>
        <value name="ADD8">
          <block type="math_number" id="9*vSgp)3B$(%[XT@qG[q">
            <field name="NUM">49</field>
          </block>
        </value>
        <value name="ADD9">
          <block type="math_number" id="RK6b:H,5l9)]~-Y@t`z`">
            <field name="NUM">71</field>
          </block>
        </value>
        <value name="ADD10">
          <block type="math_number" id="vwMR[9nP!_xO6*ijgi^D">
            <field name="NUM">63</field>
          </block>
        </value>
        <value name="ADD11">
          <block type="math_number" id="`a}?-e%j2_GCdrJXH?/p">
            <field name="NUM">53</field>
          </block>
        </value>
      </block>
    </value>
    <next>
      <block type="variables_set" id="8dzS=WhkgURv4J!_s)8A">
        <field name="VAR">intArraySpring</field>
        <value name="VALUE">
          <block type="lists_getSublist" id="#;}y-OItF(S}0]hcRI2h">
            <mutation at1="true" at2="true"></mutation>
            <field name="WHERE1">FROM_START</field>
            <field name="WHERE2">FROM_START</field>
            <value name="LIST">
              <block type="variables_get" id="R{JV-[yABDcxV?l6e3mY">
                <field name="VAR">intRainfallArray</field>
              </block>
            </value>
            <value name="AT1">
              <block type="math_number" id="Q,NDE8{aRB..72V-VtqI">
                <field name="NUM">3</field>
              </block>
            </value>
            <value name="AT2">
              <block type="math_number" id="Xv*aNsJz}2Vtts5myST%">
                <field name="NUM">5</field>
              </block>
            </value>
          </block>
        </value>
        <next>
          <block type="variables_set" id="e*ruR+}{NQcfOq2ZO+64">
            <field name="VAR">intArraySummer</field>
            <value name="VALUE">
              <block type="lists_getSublist" id="|~E#]eE+!~ivgA;agEp1">
                <mutation at1="true" at2="true"></mutation>
                <field name="WHERE1">FROM_START</field>
                <field name="WHERE2">FROM_START</field>
                <value name="LIST">
                  <block type="variables_get" id="!a~b.vc_4Rl.4AcEn|p`">
                    <field name="VAR">intRainfallArray</field>
                  </block>
                </value>
                <value name="AT1">
                  <block type="math_number" id="PBU#e)iH{vu}[OL:7fS_">
                    <field name="NUM">6</field>
                  </block>
                </value>
                <value name="AT2">
                  <block type="math_number" id="*G?t*v|]04sq~uGJhT9z">
                    <field name="NUM">8</field>
                  </block>
                </value>
              </block>
            </value>
            <next>
              <block type="text_print" id="/X,Qh8DXZ.75{N_4*Z;+">
                <value name="TEXT">
                  <shadow type="text" id="_BUd#huBi:|}Eq|hD?+a">
                    <field name="TEXT">abc</field>
                  </shadow>
                  <block type="text_join" id="Fl7|f^|sXU5(#|~Zc(6(">
                    <mutation items="5"></mutation>
                    <value name="ADD0">
                      <block type="text" id="RRK])7-SSEu?+0.%Wqn7">
                        <field name="TEXT">The rainfall (in mm) in March, April and May totals  </field>
                      </block>
                    </value>
                    <value name="ADD1">
                      <block type="math_on_list" id="lYD5Y%Dka{tFVLkU5)!O">
                        <mutation op="SUM"></mutation>
                        <field name="OP">SUM</field>
                        <value name="LIST">
                          <block type="variables_get" id="VrDyzipa{Oz88%.,xwt%">
                            <field name="VAR">intArraySpring</field>
                          </block>
                        </value>
                      </block>
                    </value>
                    <value name="ADD2">
                      <block type="text" id="ted-0AN$=o9L?ndXXV[f">
                        <field name="TEXT"> &lt;br&gt;and in June, July and August it  totals </field>
                      </block>
                    </value>
                    <value name="ADD3">
                      <block type="math_on_list" id="dUDY`tJmn$u*%++EeNB=">
                        <mutation op="SUM"></mutation>
                        <field name="OP">SUM</field>
                        <value name="LIST">
                          <block type="variables_get" id="S5aa.O8mH:XUGS8Qqz/x">
                            <field name="VAR">intArraySummer</field>
                          </block>
                        </value>
                      </block>
                    </value>
                    <value name="ADD4">
                      <block type="text" id="8Ms.8b_-UCk:.8qY1+!x">
                        <field name="TEXT">.</field>
                      </block>
                    </value>
                  </block>
                </value>
                <next>
                  <block type="lists_sort" id="([RKL:hc,[4|gR|l@+G*">
                    <field name="TYPE">NUMERIC</field>
                    <field name="DIRECTION">1</field>
                    <value name="LIST">
                      <block type="variables_get" id="6vy/(S;=YU|Kq|~.@b%l">
                        <field name="VAR">intRainfallArray</field>
                      </block>
                    </value>
                    <next>
                      <block type="variables_set" id="?^s@wTp04d2@IN}]f!sF">
                        <field name="VAR">strAscendingMonthlyRainfall</field>
                        <value name="VALUE">
                          <block type="text" id="~H*2pKy9|Dl;pN9$W.YA">
                            <field name="TEXT">Ascending monthly rainfall: </field>
                          </block>
                        </value>
                        <next>
                          <block type="controls_forEach" id="8eT67sZhhWN?@_yBnn;%">
                            <field name="VAR">i</field>
                            <value name="LIST">
                              <block type="variables_get" id="p#}7uaZpNwUw=5eus3s[">
                                <field name="VAR">intRainfallArray</field>
                              </block>
                            </value>
                            <statement name="DO">
                              <block type="text_append" id="7R=@Q0C:4RXTq*hgjt-O">
                                <field name="VAR">strAscendingMonthlyRainfall</field>
                                <value name="TEXT">
                                  <shadow type="text" id="g0C?6rHTq=mARvTh[l5w">
                                    <field name="TEXT"></field>
                                  </shadow>
                                  <block type="text_join" id="]DEh16WBVRhq|]tAa6LD">
                                    <mutation items="2"></mutation>
                                    <value name="ADD0">
                                      <block type="variables_get" id="XLKBuU]5fBP]uCfzByZV">
                                        <field name="VAR">i</field>
                                      </block>
                                    </value>
                                    <value name="ADD1">
                                      <block type="text" id="vjMepG!3#S@vUAggV|r-">
                                        <field name="TEXT">&amp;nbsp;</field>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                              </block>
                            </statement>
                            <next>
                              <block type="text_print" id="2}jYkp_fA*0#g6F8Z4x:">
                                <value name="TEXT">
                                  <shadow type="text" id="dh)elqk|2ati{)Twu1k.">
                                    <field name="TEXT">abc</field>
                                  </shadow>
                                  <block type="variables_get" id="QNFg5Jqw{H+R+{o.Mmx=">
                                    <field name="VAR">strAscendingMonthlyRainfall</field>
                                  </block>
                                </value>
                              </block>
                            </next>
                          </block>
                        </next>
                      </block>
                    </next>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var strAscendingMonthlyRainfall: String;
var intRainfallArray: TIntArray;
var intArraySummer: TIntArray;
var intArraySpring: TIntArray;


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;


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


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


var SortIntFn := function (Left, Right: Integer): Integer
                 begin
                   Result := Left - Right;
                 end;
var SortIntFnDesc := function (Left, Right: Integer): Integer
                     begin
                       Result := Right - Left;
                     end;
var SortFloatFn := function(Left, Right: Float): Integer
                   begin
                     Result := if (Left - Right > 0) then 1 else -1;
                   end;
var SortFloatFnDesc := function(Left, Right: Float): Integer
                       begin
                         Result := if (Right - Left > 0) then 1 else -1;
                       end;
var SortTextFn := function(Left, Right: String): Integer
                  begin
                    Result := if (Left > Right) then 1 else -1;
                  end;
var SortTextFnDesc := function(Left, Right: String): Integer
                      begin
                        Result := if (Right > Left) then 1 else -1;
                      end;
var SortIgnoreCaseFn := function(Left, Right: String): Integer
                        begin
                          Result := if (LowerCase(Left) > LowerCase(Right)) then 1 else -1;
                        end;
var SortIgnoreCaseFnDesc := function(Left, Right: String): Integer
                            begin
                              Result := if (LowerCase(Right) > LowerCase(Left)) then 1 else -1;
                            end;


intRainfallArray := [52, 39, 35, 43, 50, 43, 41, 48, 49, 71, 63, 53];
intArraySpring := intRainfallArray.Copy(2, 5);
intArraySummer := intRainfallArray.Copy(5, 8);
Console.writeln(StrJoin([Str('The rainfall (in mm) in March, April and May totals  '), Str(SumInt(intArraySpring)), Str(' <br>and in June, July and August it  totals '), Str(SumInt(intArraySummer)), Str('.')],''));
intRainfallArray.sort(SortIntFn);
strAscendingMonthlyRainfall := 'Ascending monthly rainfall: ';
for var i in intRainfallArray do
begin
  strAscendingMonthlyRainfall += Str(i) + Str('&nbsp;');
end;
Console.writeln(strAscendingMonthlyRainfall);
    


Copy of Output

The rainfall (in mm) in March, April and May totals  128
and in June, July and August it  totals 132.
Ascending monthly rainfall: 35 39 41 43 43 48 49 50 52 53 63 71
Programming - a skill for life!

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