P1 to P2 conversion question - solved
I have a piece of parsing code that I have used successfully for a long time on a P1 but am getting strange results running the code on the P2.
pub extractfield(n, pstr, sep):result | len, val, sign, flag, k 'routine designed by Jon McPhalen ' returns decimal value of filed n ' pstr is source string/buffer ' sep is seperator character ' look for start of field len := strsize(pstr) ' maximum length to search repeat while (n > 0) if byte[pstr++] == sep ' if character is seperator --n ' decrement field count if (--len == 0) ' if end of string return 0 ' extract value val := 0 sign := 1 flag := false repeat len k := byte[pstr++] ' get character from string case k " " : if (flag) ' if already in field quit "-" : ifnot (flag) ' if not in field sign := -1 ' set sign else quit "0".."9" : val := (val * 10) + (k - "0") ' convert ascii to decimal flag := true ' mark entry to field other : quit return val * sign
When feeding a string ( n = '4' pstr = "$,900,0,0,-4,0,0" sep = ",") into the function it works perfectly until it gets to a negative entry (-4 in this case). It returns a value like 5532 instead of -4. if I remove the '* sign' from the final return statement then I get the positive value of the entry (4).
So it appears to be a problem with the "return val * sign" code. Does the P2 not process returns the same as the P1? I did try putting 'val := val * sign' just before the return statement with return only returning val but got the same response which makes me feel the problem may be in the sign variable instead. But the code is pretty straightforward so I'm not seeing where to go from here. Anyone see what I am missing?