1. For loop and a newbie
- Posted by jc at cknet.net Mar 25, 2001
- 343 views
- Last edited Mar 26, 2001
Hello, I'm not getting what I would expect out of the below For Do loop. The For loop variable 'i' initializes at 3 as I would expect, however during the first loop I would expect cmd[i] to be cmd[3] but it isn't, it starts at cmd[1]. cmd = command_line() for i = 3 to length(cmd)-2 do vcommands = cmd[i] vresult = atom(vcommands[1]) if vresult != 1 then directions() abort(0) end if end for I want to skip the first two command line parameters as they contain path data. It's as if there are two 'i' variables within the context of the same For loop running independantly........tell me that's not what's happening........ What am I missing? TIA Jim Chapman
2. Re: For loop and a newbie
- Posted by Derek Parnell <ddparnell at bigpond.com> Mar 26, 2001
- 338 views
Hi Jim, >Hello, I'm not getting what I would expect out of the below For Do >loop. The For loop variable 'i' initializes at 3 as I would expect, >however during the first loop I would expect cmd[i] to be cmd[3] >but it isn't, it starts at cmd[1]. Well a couple of things first. I'm not sure why you think that it starts at cmd[1]. Is it because the 'directions()' routine never runs when you give no command options? Also, I'm not actually sure if I understand what you are trying to do. I guess you are trying to display some help text if no command parameters were supplied. Assuming this, I'd try the code here instead ... cmd = command_line() if length(cmd) < 3 then directions() abort(0) end if But now to explain what your code is actually doing... >cmd =command_line() This copies the words on the command line to the sequence 'cmd' Assuming the command line was "abc def ghi" then cmd will be {"ex.exe","myprog","abd","def","ghi"} That is a list of 5 (five) elements. The first two are paths, and the next three are the command line words. >for i =3 to length(cmd)-2 do This starts a loop. The first 'i' will be three and the last 'i' will be the number of elements in cmd minus 2. In our example this means that 'i' will range from 3 to 3 (5 - 2). >vcommands =cmd[i] This copies the i'th word into sequence 'vcommands' >vresult =atom(vcommands[1]) This test the first element in vcommands to see if its an atom or not. Because 'cmds' contains a list of words (strings) then all the elements of each string is an atom, so this test will always be true. That is, the first element of vcommands is always an atom (the first character in the word). >if vresult !=1 then This test that the examined character was an atom. >directions() This executes the directions() procedure. >abort(0) This exits the program. >end if >end for So in a nutshell, your program will only test the commandline words if there is at least 3 of them and it will never run the directions routine because the first element of every word is always an atom. >I want to skip the first two command line parameters as they contain >path data. It's as if there are two 'i' variables within the context >of the same For loop running independantly........tell me that's not >what's happening........ That's not happening. > What am I missing? Not sure. Maybe still confused about what a sequence is? cheers, ------ Derek Parnell Melbourne, Australia "To finish a job quickly, go slower."
3. Re: For loop and a newbie
- Posted by Irv Mullins <irvm at ellijay.com> Mar 26, 2001
- 326 views
On Mon, 26 Mar 2001, jc at cknet.net wrote: > Hello, I'm not getting what I would expect out of the below For Do loop. > The For loop variable 'i' initializes at 3 as I would expect, however during > the first loop I would expect cmd[i] to be cmd[3] but it isn't, it starts at > cmd[1]. > > cmd command_line() > for i 3 to length(cmd)-2 do > vcommands cmd[i] > vresult atom(vcommands[1]) > if vresult ! 1 then > directions() > abort(0) > end if > end for > It would be hard to beat Derek's fine reply, but I'm curious why you are using atom(vcommands[1])? The command line parameters are _always_ strings, so, even if you type "ex myprog One 2 Three 4", each item is a string. Suppose you want to separate the actual string parameters from those which contain only a number; Here's a test program that will display the parameters, and decide if each is a number or not: Execute it with a mix of parameters, something like this: ex myprog One 2 Three 56 Nine "45" include get.e sequence cmd object result cmd = command_line() for i = 1 to length(cmd) do printf(1,"Command line parameter #%d was %s\n",{i,cmd[i]}) result = value(cmd[i]) if result[1] = GET_SUCCESS then printf(1," Hey, parameter %d is a number = %d\n",{i,result[2]}) end if end for -- Regards, Irv