Div and Mod with Variables

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

CElwell
Posts: 38
Joined: Thu Feb 04, 2016 6:29 pm

Div and Mod with Variables

Post by CElwell » Tue May 03, 2016 6:16 pm

How do you use div and mod with variables because I get errors with my code even tough the variable produces a number value.

Code: Select all

      put revXMLNodeContents( tInfo, "ArrayOfXmlNode/"&tChild&"/"&tAdoptable&"/Age") into tAge
      tAge div 12
      put it into tYears
      tAge mod 12
      put it into tMonths
      put "Age: " & tYears & " " & tMonths & return after tData 

      put tData & return after tOutput
      set the text of field "tData" to tOutput
Thank you!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Div and Mod with Variables

Post by dunbarx » Tue May 03, 2016 6:23 pm

Hi.

You have syntax errors, not operator errors. I started reading your code, and threw an error at line 2. You are asking LC to find a handler with the name "tAge". There isn't one. I think you want to:

Code: Select all

put tAge div 12 into someOtherVariable_EvenIncludingtAgeItselfIfYouWant
or somesuch.

Try it, modify as you go, and see what happens.

Craig Newman

CElwell
Posts: 38
Joined: Thu Feb 04, 2016 6:29 pm

Re: Div and Mod with Variables

Post by CElwell » Tue May 03, 2016 6:28 pm

dunbarx wrote:Hi.

You have syntax errors, not operator errors. I started reading your code, and threw an error at line 2. You are asking LC to find a handler with the name "tAge". There isn't one. I think you want to:

Code: Select all

put tAge div 12 into someOtherVariable_EvenIncludingtAgeItselfIfYouWant
or somesuch.

Try it, modify as you go, and see what happens.

Craig Newman
I tried this also but I get an error with the left operand for both.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Div and Mod with Variables

Post by Klaus » Tue May 03, 2016 6:30 pm

Did you make sure that

Code: Select all

put revXMLNodeContents( tInfo, "ArrayOfXmlNode/"&tChild&"/"&tAdoptable&"/Age") into tAge
really returns a number in tAge?

CElwell
Posts: 38
Joined: Thu Feb 04, 2016 6:29 pm

Re: Div and Mod with Variables

Post by CElwell » Tue May 03, 2016 6:33 pm

Yes, currently it gives a number which is the age in months. I am trying to divide by 12 to get just the number of years and then use mod to also show the number of months in addition to the years.

For example: tAge = 71 but want it to show as 5 years and 11 months.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Div and Mod with Variables

Post by Klaus » Tue May 03, 2016 6:43 pm

Hm, the little script works as exspected in LC 8 RC1 (with age 71 hardcoded!)!?

CElwell
Posts: 38
Joined: Thu Feb 04, 2016 6:29 pm

Re: Div and Mod with Variables

Post by CElwell » Tue May 03, 2016 6:51 pm

Hm, the little script works as exspected in LC 8 RC1 (with age 71 hardcoded!)!?
I understand this, but it does not work with the variable which does display a value. Would you like me to show you more code? I am unsure why it is not working, but tAge does represent a value so...

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Div and Mod with Variables

Post by Klaus » Tue May 03, 2016 7:00 pm

Hm, any invisible chars (= garbage) in tAge?

CElwell
Posts: 38
Joined: Thu Feb 04, 2016 6:29 pm

Re: Div and Mod with Variables

Post by CElwell » Tue May 03, 2016 7:49 pm

No...here is a screenshot though
Attachments
1.png

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Div and Mod with Variables

Post by dunbarx » Tue May 03, 2016 9:27 pm

Hi.

Test tAge and verify it is a number:

Code: Select all

if tAge is a number then keepGoing
Craig

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Div and Mod with Variables

Post by SparkOut » Tue May 03, 2016 9:34 pm

Check the line calculating tMonth. You have "Age" not "tAge".

CElwell
Posts: 38
Joined: Thu Feb 04, 2016 6:29 pm

Re: Div and Mod with Variables

Post by CElwell » Tue May 03, 2016 10:09 pm

tAge does give a number as seen here: it gives numbers such as 71, 64, 70, and 51. So the problem must be somewhere with the div or mod.
Attachments
2.png

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Div and Mod with Variables

Post by dunbarx » Tue May 03, 2016 11:34 pm

Did you read Sparkout's post?

If you have a number in tAge, it will work. And if you have tAge itself, it will work.

You are now in the exalted group of those who struggle mightily, and finally discover the tiny flaw in the ointment. Or fly. This includes everyone here...

Craig

CElwell
Posts: 38
Joined: Thu Feb 04, 2016 6:29 pm

Re: Div and Mod with Variables

Post by CElwell » Tue May 03, 2016 11:46 pm

Yes, I accidently deleted it when trying to copy the code onto this forum and I fixed it but that is not the problem.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Div and Mod with Variables

Post by dunbarx » Wed May 04, 2016 2:20 am

Hmmm.

What happens if you throw in a line just before the "div" line, where, say, you:

Code: Select all

put tAge + 4 into temp
Does that compute? Because if it does, then:

Code: Select all

put tAge div 4 into temp
will as well.

I cannot wait to find out where the gremlin lies...

Craig

Post Reply