Converting strings to integers

We could not find a block for converting a string to an integer. Rather than introduce this functionality by changing or adding a block (which would have caused problems when generating languages other than Smart Pascal from your Blockly programs) we provide a method for achieving the conversion with the existing blocks. We use our Int function in our conversion of an array of string to an array of integer.

The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="procedures_defreturn" id="5^%nGyk.xeN/-30P(0M," x="112" y="38">
    <mutation>
      <arg name="strDigit"></arg>
    </mutation>
    <field name="NAME">CharToDigit</field>
    <comment pinned="false" h="80" w="160">Converts single character string to digit</comment>
    <statement name="STACK">
      <block type="controls_for" id="HxnO|}:0:TV~`KzfpesL">
        <field name="VAR">i</field>
        <value name="FROM">
          <shadow type="math_number" id="NW%K5uv1,x?d~k,}bY#P">
            <field name="NUM">0</field>
          </shadow>
        </value>
        <value name="TO">
          <shadow type="math_number" id="ym.~?ppG17/I@U!mMY;S">
            <field name="NUM">9</field>
          </shadow>
        </value>
        <value name="BY">
          <shadow type="math_number" id="soI4mAw$n7F18o6.dWqM">
            <field name="NUM">1</field>
          </shadow>
        </value>
        <statement name="DO">
          <block type="controls_if" id="85it;Rz?;w(.KuMwXT2W">
            <value name="IF0">
              <block type="logic_compare" id="KsD;r*8(]*~,Sdb+U@VR">
                <field name="OP">EQ</field>
                <value name="A">
                  <block type="text_join" id="ewS+W5u*8SMu-Q/VKL1g">
                    <mutation items="1"></mutation>
                    <value name="ADD0">
                      <block type="variables_get" id="@0)?vn)fL)dwM]j*mSLs">
                        <field name="VAR">i</field>
                      </block>
                    </value>
                  </block>
                </value>
                <value name="B">
                  <block type="variables_get" id="O]B2[2V_DLa1@MI=v_LD">
                    <field name="VAR">strDigit</field>
                  </block>
                </value>
              </block>
            </value>
            <statement name="DO0">
              <block type="variables_set" id="}YdjkO%y95DyTZj|~Nmw">
                <field name="VAR">intDigit</field>
                <value name="VALUE">
                  <block type="variables_get" id=";il@iz8E^SUDtKCo8a`k">
                    <field name="VAR">i</field>
                  </block>
                </value>
                <next>
                  <block type="controls_flow_statements" id="#lf8PyTqWEq|ReQnp7IX">
                    <field name="FLOW">BREAK</field>
                  </block>
                </next>
              </block>
            </statement>
          </block>
        </statement>
      </block>
    </statement>
    <value name="RETURN">
      <block type="variables_get" id="TUKBQ+YT._gE[I5[ckCw">
        <field name="VAR">intDigit</field>
      </block>
    </value>
  </block>
  <block type="procedures_defreturn" id="9;Q+leVY@WfvmAxLr{HC" x="113" y="263">
    <mutation>
      <arg name="strNum"></arg>
    </mutation>
    <field name="NAME">Int</field>
    <comment pinned="false" h="80" w="160">Converts string to integer</comment>
    <statement name="STACK">
      <block type="variables_set" id="=uDPxV8r{AdC1gsl!I,U">
        <field name="VAR">boolNeg</field>
        <value name="VALUE">
          <block type="logic_boolean" id="8LHc}~EGmlyI]/%uce$0">
            <field name="BOOL">FALSE</field>
          </block>
        </value>
        <next>
          <block type="controls_if" id="4I2)px`{q4-#b7O=]qeJ">
            <value name="IF0">
              <block type="logic_compare" id="bL!c9J~@rnK^i%-%l)ty">
                <field name="OP">EQ</field>
                <value name="A">
                  <block type="text_charAt" id="#0Tyz~yHt*;6LUk4XP.4">
                    <mutation at="false"></mutation>
                    <field name="WHERE">FIRST</field>
                    <value name="VALUE">
                      <block type="variables_get" id="bKs5xgDn8.u7tRsfT;`W">
                        <field name="VAR">strNum</field>
                      </block>
                    </value>
                  </block>
                </value>
                <value name="B">
                  <block type="text" id="Ntg^,R6+LAL6!i*i2aDL">
                    <field name="TEXT">-</field>
                  </block>
                </value>
              </block>
            </value>
            <statement name="DO0">
              <block type="variables_set" id="?~23qQ@p%f|P{?*MJbQ[">
                <field name="VAR">boolNeg</field>
                <value name="VALUE">
                  <block type="logic_boolean" id="Gu9@)]$.:N6s0!uH~C6r">
                    <field name="BOOL">TRUE</field>
                  </block>
                </value>
                <next>
                  <block type="variables_set" id="%#i;g!kp7Cp.G30sDsmY">
                    <field name="VAR">strNum</field>
                    <value name="VALUE">
                      <block type="text_getSubstring" id="]m@N|`El3%QYMug`^#~y">
                        <mutation at1="true" at2="false"></mutation>
                        <field name="WHERE1">FROM_START</field>
                        <field name="WHERE2">LAST</field>
                        <value name="STRING">
                          <block type="variables_get" id="3OIM0YD+;._?U1w|%u8*">
                            <field name="VAR">strNum</field>
                          </block>
                        </value>
                        <value name="AT1">
                          <block type="math_number" id="o9;)C3(`B?7GUWjdOz5(">
                            <field name="NUM">2</field>
                          </block>
                        </value>
                      </block>
                    </value>
                  </block>
                </next>
              </block>
            </statement>
            <next>
              <block type="variables_set" id="xbCuS?wp8mCd8BdR..`g">
                <field name="VAR">intNum</field>
                <value name="VALUE">
                  <block type="math_number" id="xSl99;E]+X=a$WpKIz}|">
                    <field name="NUM">0</field>
                  </block>
                </value>
                <next>
                  <block type="controls_for" id="D_[!8L+BC0M-76uuP8~c">
                    <field name="VAR">i</field>
                    <value name="FROM">
                      <shadow type="math_number" id="KjupG)DmU8IDAAJmM]OW">
                        <field name="NUM">1</field>
                      </shadow>
                    </value>
                    <value name="TO">
                      <shadow type="math_number" id="/[D!Qt6AY!m=tQXl,rMD">
                        <field name="NUM">10</field>
                      </shadow>
                      <block type="text_length" id="PmPlXS(99^^8TFH2xM6l">
                        <value name="VALUE">
                          <shadow type="text" id="H/W%X7U+(b0A`yL_[F6D">
                            <field name="TEXT">abc</field>
                          </shadow>
                          <block type="variables_get" id="BD5Bit$*[BaifNl6c_uB">
                            <field name="VAR">strNum</field>
                          </block>
                        </value>
                      </block>
                    </value>
                    <value name="BY">
                      <shadow type="math_number" id="Z;!xp9KgwPIuLx8c@3-3">
                        <field name="NUM">1</field>
                      </shadow>
                    </value>
                    <statement name="DO">
                      <block type="variables_set" id="y[jS`@RY~/:/`fZ@B$Ho">
                        <field name="VAR">strCurrentChar</field>
                        <value name="VALUE">
                          <block type="text_charAt" id="(gMYZ.$VIED4-Z+@:-Fw">
                            <mutation at="true"></mutation>
                            <field name="WHERE">FROM_START</field>
                            <value name="VALUE">
                              <block type="variables_get" id="UQC#NYr9^lv^pU_jf*7+">
                                <field name="VAR">strNum</field>
                              </block>
                            </value>
                            <value name="AT">
                              <block type="variables_get" id="v2ae;]BqKZH*{}$C*Evi">
                                <field name="VAR">i</field>
                              </block>
                            </value>
                          </block>
                        </value>
                        <next>
                          <block type="variables_set" id="R45,@kP1HJ:AznL$muS:">
                            <field name="VAR">intMultiplier</field>
                            <value name="VALUE">
                              <block type="math_round" id="g;X4Mi$.$A/av^DntM-y">
                                <field name="OP">ROUND</field>
                                <value name="NUM">
                                  <shadow type="math_number" id="}IE1uPg25@j%X2yO}#V;">
                                    <field name="NUM">3.1</field>
                                  </shadow>
                                  <block type="math_single" id="170Xf$[=iLo)YtP)$/T=">
                                    <field name="OP">POW10</field>
                                    <value name="NUM">
                                      <shadow type="math_number" id="ACXu3$|CE#S3(2;Yc_iv">
                                        <field name="NUM">9</field>
                                      </shadow>
                                      <block type="math_arithmetic" id="V~GG`JMt:w09Q:(y0*?e">
                                        <field name="OP">MINUS</field>
                                        <value name="A">
                                          <shadow type="math_number" id="hLFxm!xa$7*_$c%(`=i/">
                                            <field name="NUM">1</field>
                                          </shadow>
                                          <block type="text_length" id="M,hTTb,qW]]PnE$S)WLS">
                                            <value name="VALUE">
                                              <shadow type="text" id="WpAh84$}70}fN!gKdRIW">
                                                <field name="TEXT">abc</field>
                                              </shadow>
                                              <block type="variables_get" id="d$5YXZqC9rw;H(o7Qa6/">
                                                <field name="VAR">strNum</field>
                                              </block>
                                            </value>
                                          </block>
                                        </value>
                                        <value name="B">
                                          <shadow type="math_number" id="fPo?jzW(g{z[$H_,UI.t">
                                            <field name="NUM">1</field>
                                          </shadow>
                                          <block type="variables_get" id="Y/cnN(Cv!o?^w/Vz`Nt^">
                                            <field name="VAR">i</field>
                                          </block>
                                        </value>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <next>
                              <block type="math_change" id="V|kC/A!H+,cwkpczuwZc">
                                <field name="VAR">intNum</field>
                                <value name="DELTA">
                                  <shadow type="math_number" id="Fs/`6[nDki~}vEsx=@$+">
                                    <field name="NUM">1</field>
                                  </shadow>
                                  <block type="math_arithmetic" id="DSPs#={C_P:R,Fi)KK;p">
                                    <field name="OP">MULTIPLY</field>
                                    <value name="A">
                                      <shadow type="math_number" id="V+0rA0c`3p,,O7GuzOr-">
                                        <field name="NUM">1</field>
                                      </shadow>
                                      <block type="procedures_callreturn" id=";mn}FX!+]3kEuBm=e2b,">
                                        <mutation name="CharToDigit">
                                          <arg name="strDigit"></arg>
                                        </mutation>
                                        <value name="ARG0">
                                          <block type="variables_get" id="bU+^2*Br=_DMRi?E~uRm">
                                            <field name="VAR">strCurrentChar</field>
                                          </block>
                                        </value>
                                      </block>
                                    </value>
                                    <value name="B">
                                      <shadow type="math_number" id="o)VARrFspGY!N-8-eDd[">
                                        <field name="NUM">1</field>
                                      </shadow>
                                      <block type="variables_get" id="MYpeSy/@Ts3AeuIXE_RZ">
                                        <field name="VAR">intMultiplier</field>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                              </block>
                            </next>
                          </block>
                        </next>
                      </block>
                    </statement>
                    <next>
                      <block type="controls_if" id="df)c~RyqKy0k7+5mfG:c">
                        <value name="IF0">
                          <block type="logic_compare" id="^e6@pclg3Wd9@3^e0xK=">
                            <field name="OP">EQ</field>
                            <value name="A">
                              <block type="variables_get" id=",^F.=z!NEGDL+Gl8rB!}">
                                <field name="VAR">boolNeg</field>
                              </block>
                            </value>
                            <value name="B">
                              <block type="logic_boolean" id="iQC][*sB8ah4pP[_4kOh">
                                <field name="BOOL">TRUE</field>
                              </block>
                            </value>
                          </block>
                        </value>
                        <statement name="DO0">
                          <block type="variables_set" id="RP__.C|IsZjO=c{;I1P8">
                            <field name="VAR">intNum</field>
                            <value name="VALUE">
                              <block type="math_single" id="o8%vcq+|0sDSUErX3v.G">
                                <field name="OP">NEG</field>
                                <value name="NUM">
                                  <shadow type="math_number" id="X;6MH[y^SK5v}p,EhZ[U">
                                    <field name="NUM">9</field>
                                  </shadow>
                                  <block type="variables_get" id="WJAP[E~z)/F0_/MO;4uu">
                                    <field name="VAR">intNum</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                          </block>
                        </statement>
                      </block>
                    </next>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </statement>
    <value name="RETURN">
      <block type="variables_get" id="7sr*pnw%AVvV_wOC%sN)">
        <field name="VAR">intNum</field>
      </block>
    </value>
  </block>
  <block type="variables_set" id="tFW!S@-;`k]mB)V)-06@" x="113" y="738">
    <field name="VAR">strInput</field>
    <value name="VALUE">
      <block type="text_prompt_ext" id="0peaz5RrrRa+o~sA|!9;">
        <mutation type="TEXT"></mutation>
        <field name="TYPE">TEXT</field>
        <value name="TEXT">
          <shadow type="text" id="p-hMt%fVst[]ueClgwn^">
            <field name="TEXT">Please enter comma-separated list of  integers.</field>
          </shadow>
        </value>
      </block>
    </value>
    <next>
      <block type="variables_set" id="@m;}NqN{pDh)K$T/Jh:`">
        <field name="VAR">strInputArray</field>
        <value name="VALUE">
          <block type="lists_split" id="SZS$r?v|evu%!hNOr$)0">
            <mutation mode="SPLIT"></mutation>
            <field name="MODE">SPLIT</field>
            <value name="INPUT">
              <block type="variables_get" id="i`CtudW~C+9CI7:4$zr0">
                <field name="VAR">strInput</field>
              </block>
            </value>
            <value name="DELIM">
              <shadow type="text" id="Wga)NV8mvP#gf=+PDnPH">
                <field name="TEXT">,</field>
              </shadow>
            </value>
          </block>
        </value>
        <next>
          <block type="variables_set" id="18X.[@K1z|OZxs~CGWY_">
            <field name="VAR">intInputArray</field>
            <value name="VALUE">
              <block type="lists_repeat" id="VPwYdlLeb~Z565NR+K_R">
                <value name="ITEM">
                  <block type="math_number" id="CQUpATjN{!97U`q2ZH_R">
                    <field name="NUM">0</field>
                  </block>
                </value>
                <value name="NUM">
                  <shadow type="math_number" id="BZZ5^JZRV}e+072x|_nh">
                    <field name="NUM">5</field>
                  </shadow>
                  <block type="lists_length" id="][~z;o7q49ldD=2%0A,u">
                    <value name="VALUE">
                      <block type="variables_get" id="`rz?N/W]5D7x!TuMt9}J">
                        <field name="VAR">strInputArray</field>
                      </block>
                    </value>
                  </block>
                </value>
              </block>
            </value>
            <next>
              <block type="controls_for" id="BD!Atdq5s8kaK?%rWD$P">
                <field name="VAR">i</field>
                <value name="FROM">
                  <shadow type="math_number" id="1%qri~%_B#Yd}:Q[~b95">
                    <field name="NUM">1</field>
                  </shadow>
                </value>
                <value name="TO">
                  <shadow type="math_number" id="G0a}r%q3HZmcK4x={;lm">
                    <field name="NUM">10</field>
                  </shadow>
                  <block type="lists_length" id="*f+7TqAC*cTk$W-ZT7Y[">
                    <value name="VALUE">
                      <block type="variables_get" id="b8)o%4Rsy^UFea(yKG-a">
                        <field name="VAR">strInputArray</field>
                      </block>
                    </value>
                  </block>
                </value>
                <value name="BY">
                  <shadow type="math_number" id="Ag4mQHv:]vvZa6;zM:#O">
                    <field name="NUM">1</field>
                  </shadow>
                </value>
                <statement name="DO">
                  <block type="lists_setIndex" id="O4-iQ!Z?4,RUY7Vj.AP_">
                    <mutation at="true"></mutation>
                    <field name="MODE">SET</field>
                    <field name="WHERE">FROM_START</field>
                    <value name="LIST">
                      <block type="variables_get" id="PF%$KYfTG0C!ET?HbgXv">
                        <field name="VAR">intInputArray</field>
                      </block>
                    </value>
                    <value name="AT">
                      <block type="variables_get" id="$S2l]mz:wX2DaiDt[4LR">
                        <field name="VAR">i</field>
                      </block>
                    </value>
                    <value name="TO">
                      <block type="procedures_callreturn" id="oN-q;A_o4cDu^at7{#Qr">
                        <mutation name="Int">
                          <arg name="strNum"></arg>
                        </mutation>
                        <value name="ARG0">
                          <block type="lists_getIndex" id=":uh#M7_dIA#Y~`sKjYFW">
                            <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="yz]KxS=[51%01U0{l+4A">
                                <field name="VAR">strInputArray</field>
                              </block>
                            </value>
                            <value name="AT">
                              <block type="variables_get" id="y!5E?/rP~QW=|7B%pO(N">
                                <field name="VAR">i</field>
                              </block>
                            </value>
                          </block>
                        </value>
                      </block>
                    </value>
                  </block>
                </statement>
                <next>
                  <block type="text_print" id="9c3?)pF7L=`M9PBsZ=91">
                    <value name="TEXT">
                      <shadow type="text" id="bz#GorZ2ZvXS@|4y{M8#">
                        <field name="TEXT">abc</field>
                      </shadow>
                      <block type="text_join" id="p{`#+GtNYjhIex:oSsw]">
                        <mutation items="6"></mutation>
                        <value name="ADD0">
                          <block type="text" id="d]dR[4l]du0G2.N4_j$%">
                            <field name="TEXT">Your list </field>
                          </block>
                        </value>
                        <value name="ADD1">
                          <block type="variables_get" id="suAyl*wq`;Q6tvg]ddmG">
                            <field name="VAR">strInput</field>
                          </block>
                        </value>
                        <value name="ADD2">
                          <block type="text" id="k$Xr:Sh`mA+qIvBY5:`6">
                            <field name="TEXT"> has sum </field>
                          </block>
                        </value>
                        <value name="ADD3">
                          <block type="math_on_list" id="9tE$1-UVVMAT,@(I{pW(">
                            <mutation op="SUM"></mutation>
                            <field name="OP">SUM</field>
                            <value name="LIST">
                              <block type="variables_get" id="cm/sOT=i4U6;VF44@uC7">
                                <field name="VAR">intInputArray</field>
                              </block>
                            </value>
                          </block>
                        </value>
                        <value name="ADD4">
                          <block type="text" id="t4E9$;/b0wGvYxJqLd=x">
                            <field name="TEXT"> and max </field>
                          </block>
                        </value>
                        <value name="ADD5">
                          <block type="math_on_list" id="4:{goEne`SlYNmV00F7M">
                            <mutation op="MAX"></mutation>
                            <field name="OP">MAX</field>
                            <value name="LIST">
                              <block type="variables_get" id="am{TYyeDk[ohp~idc=sR">
                                <field name="VAR">intInputArray</field>
                              </block>
                            </value>
                          </block>
                        </value>
                      </block>
                    </value>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var boolNeg: Boolean;
var intDigit: Integer;
var intInputArray: TIntArray;
var intMultiplier: Integer;
var intNum: Integer;
var strCurrentChar: String;
var strDigit: String;
var strInput: String;
var strInputArray: TStrArray;
var strNum: String;


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;


{
  Converts single character string to digit
}
function CharToDigit(strDigit: String): Integer;
begin
  for var i := 0 to 9 do
  begin
    if ((Str(i) = strDigit)) then
    begin
      intDigit := i;
      break;
    end;
  end;
  Result := intDigit;
end;


{
  Converts string to integer
}
function Int(strNum: String): Integer;
begin
  boolNeg := false;
  if ((LeftStr(strNum, 1) = '-')) then
  begin
    boolNeg := true;
    strNum := copy(strNum, 1 + 1, strNum.length - 1);
  end;
  intNum := 0;
  for var i := 1 to length(strNum) step 1 do
  begin
    strCurrentChar := CharAt(strNum, (i - 1) + 1);
    intMultiplier := Round(Power(10,length(strNum) - i));
    intNum += CharToDigit(strCurrentChar) * intMultiplier;
  end;
  if ((boolNeg = true)) then
  begin
    intNum := -intNum;
  end;
  Result := intNum;
end;


function RepeatArray(Value: Variant; n: integer): Variant;
begin
  var Arr := [];
  for var i := 0 to n - 1 do
    Arr[i] := Value;
  Result := Variant(Arr);
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;


function MaxValueFloat(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 MaxValueInt(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;



strInput := prompt('Please enter comma-separated list of  integers.');
strInputArray := StrSplit(strInput, ',');
intInputArray := TIntArray(RepeatArray(0, strInputArray.length));
for var i := 1 to strInputArray.length step 1 do
begin
  intInputArray[(i - 1)] := Int(strInputArray[(i - 1)]);
end;
Console.writeln(StrJoin([Str('Your list '), Str(strInput), Str(' has sum '), Str(SumInt(intInputArray)), Str(' and max '), Str(MaxValueInt(intInputArray))],''));
    

Copy of Output

Your list 21,43,15,-5,99 has sum 173 and max 99
Programming - a skill for life!

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