Jump to content

error when adding variables


Stephen Shafer

Recommended Posts

I am trying to get this LISP routine going and I am having an issue adding two variables together. When I run the program it returns

 ; error: bad argument type: numberp: nil 

here is the code segment in question

fa1    (+ fa1 inc)  
fa2    (+ fa2 inc) 

fa1, fa2, and inc are all supposed to be defined as integers and for some reason i keep getting that error and i dont understand why. Basically what is supposed to happen is fa1 gets define as (arbitrarily) 5 and fa2 will be 6, with inc being, say, 7. so basically fa1 and fa2 are both getting "incremented" by 7. Everything else seems to be going smoothly but i cant manage to get this small part to work. Any help is appreciated. Thanks

Link to comment
Share on other sites

The error that you are returning seems that there is a variable in your coding that is perhaps defined incorrectly - i.e. as possibly a string.

 

The code that you have posted seems to be correct:

(setq fa1 (+ fa1 inc)
       fa2 (+ fa2 inc)
) ; end setq

 

But if you could post the rest of the LISP you are working on, there may be an error elsewhere.

Link to comment
Share on other sites

@Lee Mac Thanks for the input, here's the rest of the code

 (setq ename  	(ssname ss index)
  index  	(1+ index)
         Fass  	(getattval ename "FIBER-ASS")
)
               (setq Fspl 	(splstr Fass "-")
		inc	(last Fspl)
		inc	(atoi inc)
		fa1	(car fspl)
		fa2	(last fspl)
		fa1	(+ fa1 inc)
		fa2	(+ fa2 inc)
		fa1 	(itoa fa1)
		fa2	(itoa fa2)
		Fincd	(strcat fa1 "-" fa2)

Link to comment
Share on other sites

This code:

(setq fa1 (+ fa1 inc)

 

will give you an error if you haven't set fa1 to some value; fa1 is 'nil initially if not set to something, you can't add an increment to nil and thus an error.

 

Yop probably want to set fa1 to 0 initially?

Link to comment
Share on other sites

What does the splstr function do to the Fass variable when it is passed through it? Does it split the string and retun it as a list of strings?

 

eg (splstr "100-2-5" "-") returns ("100" "2" "5")

 

Try

 

 

 (setq ename  	(ssname ss index)
  index  	(1+ index)
         Fass  	(getattval ename "FIBER-ASS")
)
               (setq Fspl 	(splstr Fass "-")
		inc	(last Fspl)
		inc	(atoi inc)
		fa1	[color="Red"](atoi [/color](car fspl)[color="red"])[/color] ;covert to integer
		fa2	[color="red"](atoi [/color](last fspl)[color="red"])[/color];covert to integer

		fa1	(+ fa1 inc)
		fa2	(+ fa2 inc)
		fa1 	(itoa fa1)
		fa2	(itoa fa2)
		Fincd	(strcat fa1 "-" fa2)

 

 

 

Jammie

Link to comment
Share on other sites

getattval gets an attribute value and splstr splits a string based on, in this case, a hyphen and creates a list.

@CarlB Is the line fa1 (car fspl) not sufficient enough to properly assign a value to the variable in order to perform the math?

@Jammie that seems to work. Well, I mean its returning the correct value now. I just cant manage to get the rest of the routine to work :(. Anyways, thanks guys

Link to comment
Share on other sites

Does (car faspl) return a number? If 'faspl' is a string then (car faspl) wil generate an error. After running the code and it 'errors', check what the value of faspl & fa1 are (and if OK keep checking...)

Link to comment
Share on other sites

shorter (almost exactly as Stephen posted):

 

(if (

 

It's often missed that lisp allows more than 2 items in an equality/inequality. For example this is legal

 

(= 1 2 3 4 5) -> nil

( T

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...