Re: splitting sequences
- Posted by Andy Serpa <ac at onehorseshy.com> Nov 28, 2004
- 493 views
Jacen wrote: > > Thanks cklester, I shoulda seen that. But now I have another problem. > I have a procedure exec_cmd that tests if the cmd is in the keywords > sequence.If it doesn't find it there, then it splits the input, and tests > the first string(cmd[1]). But when it returns from the split procedure, cmd > changes back to what it was before it was splitted. See code below. Is this > another simple soultion I have overlooked again? > > }}} <eucode> > > global procedure split_cmd(sequence local_cmd) > integer j > sequence w > > j = find(' ', cmd) > if j > 0 then > w = cmd > cmd = {{},{}} > cmd[1] =w[1..j-1] > cmd[2] = w[j+1..length(w)] > end if > end procedure > > while 1 do > > cmd = gets(0) > cmd = cmd[1..length(cmd)-1]-- take off last character(\n) > cmd_loc = find(cmd, keywords) > if cmd_loc > 0 then > if cmd_loc > 1 and cmd_loc < 8 then -- if player is moving, let > move_self() handle it > move_self(cmd) > cur_loc = Tatooine[x][y] > num_exits = length(cur_loc[exits]) > look() > elsif cmd_loc = find("look",keywords) then > look() > end if > end if > split_cmd(cmd) > if length(cmd) = 2 then > if equal(w[1],"greet") then > if find(w[2], cur_loc[people][NAME]) then > puts(1, cur_loc[people][GREET_RESP]) > end if > end if > end if > > end while > </eucode> {{{ > In Euphora everything is "passed by value", so when the procedure split_cmd alters the value of cmd, it only does so locally, within the scope of that procedure only (because cmd is declared as a parameter). So there are two solutions. Declare "cmd" globally somewhere above the procedure split_cmd, and don't use a parameter at all in your procedure. For instance: sequence cmd global procedure split_cmd() integer j sequence w j = find(' ', cmd) if j > 0 then w = cmd cmd = {{},{}} cmd[1] =w[1..j-1] cmd[2] = w[j+1..length(w)] end if end procedure The other, and possibly better solution, is to make split_cmd a function, so it returns the altered copy of cmd: global function split_cmd(sequence cmd) integer j sequence w j = find(' ', cmd) if j > 0 then w = cmd cmd = {{},{}} cmd[1] =w[1..j-1] cmd[2] = w[j+1..length(w)] end if return cmd end function And then alter the call in the loop to: cmd = split_cmd(cmd) I see now that you are using "w" in the loop as well below the call the split_cmd, but you also declare w in the split_cmd procedure locally. Which means that the line: if equal(w[1],"greet") then will fail because the program has no knowledge of w at that point. You would need to declare w outside the procedure split_cmd as well...