Re: For loop and a newbie

new topic     » goto parent     » topic index » view thread      » older message » newer message

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

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu