/* StrayNumSum - Adds the elements in a numerical array-string DESCRIPTION Adds all numbers in Stray (which must not contain non-numerical elements). Simple math expressions like +, -, *, /, ^ and % are allowed (no parentheses at the moment). Elements are defined by two seperators as ASCII coded characters: isep1 defaults to 32 (= space), isep2 defaults to 9 (= tab). If just one seperator is used, isep2 equals isep1. Requires the UDOs StrayLen and StrayGetEl. SYNTAX isum StrayNumSum Stray [, isep1 [, isep2]]] INITIALIZATION Stray - a string as array isep1 - the first seperator (default=32: space) isep2 - the second seperator (default=9: tab) CREDITS joachim heintz 2010 */ opcode StrayNumSum, i, Sjj ;requires the UDOs StrayLen and StrayGetEl Stray, isepA, isepB xin isep1 = (isepA == -1 ? 32 : isepA) isep2 = (isepA == -1 && isepB == -1 ? 9 : (isepB == -1 ? isep1 : isepB)) Sep1 sprintf "%c", isep1 Sep2 sprintf "%c", isep2 isumtot = 0 ilen StrayLen Stray, isep1, isep2 if ilen == 0 igoto end indx = 0 loop: Snum StrayGetEl Stray, indx, isep1, isep2 ;test if Snum is an math expression isum strindex Snum, "+"; sum idif strindex Snum, "-"; difference ipro strindex Snum, "*"; product irat strindex Snum, "/"; ratio ipow strindex Snum, "^"; power imod strindex Snum, "%"; modulo if ipow > -1 then ifirst strindex Snum, "^" S1 strsub Snum, 0, ifirst S2 strsub Snum, ifirst+1 iratio strindex S2, "/" ifirst strtod S1 if iratio == -1 then isec strtod S2 else Snumer strsub S2, 0, iratio Sdenom strsub S2, iratio+1 inumer strtod Snumer idenom strtod Sdenom isec = inumer / idenom endif inum = ifirst ^ isec elseif imod > -1 then ifirst strindex Snum, "%" S1 strsub Snum, 0, ifirst S2 strsub Snum, ifirst+1 ifirst strtod S1 isec strtod S2 inum = ifirst % isec elseif ipro > -1 then ifirst strindex Snum, "*" S1 strsub Snum, 0, ifirst S2 strsub Snum, ifirst+1 ifirst strtod S1 isec strtod S2 inum = ifirst * isec elseif irat > -1 then ifirst strindex Snum, "/" S1 strsub Snum, 0, ifirst S2 strsub Snum, ifirst+1 ifirst strtod S1 isec strtod S2 inum = ifirst / isec elseif isum > -1 then ifirst strindex Snum, "+" S1 strsub Snum, 0, ifirst S2 strsub Snum, ifirst+1 ifirst strtod S1 isec strtod S2 inum = ifirst + isec elseif idif > -1 then ifirst strrindex Snum, "-";(last occurrence: -3-4 is possible, but not 3--4) S1 strsub Snum, 0, ifirst S2 strsub Snum, ifirst+1 ifirst strtod S1 isec strtod S2 inum = ifirst - isec else inum strtod Snum endif isumtot = isumtot + inum loop_lt indx, 1, ilen, loop end: xout isumtot endop