Normal Topic Incorporating Variables in Xlookup Statements (Read 1287 times)
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Incorporating Variables in Xlookup Statements
Feb 29th, 2004 at 4:11am
Print Post Print Post  
I was trying to use x-family functions in loops and that required me to use variable in the xlookup statements. I am not having favourable results so far. While debugging, I stumbled upon apparent inconsistency when using variable in xlookup function. In the following, you would expect to have the same results, but no... Q2 is a number field with value like 20040228001 (11 digits to be exact)

var n as Int = 999999999999

WriteLn ("Value without variable = " + @xlookupr (@fn, 999999999999, "Vital!Q2", "Date1"))

writeLn ("Value with Variable = " + @xlookupr (@fn, n, "Vital!Q2", "Date1"))

The first statement consistently produced the right results, while the second one did not return the value at all or when I increased 9s in the n values to say 14 9s, it gave me sometimes value which is not the highest. That to some extent explained why my loop function failed to work.

Is there something I am doing wrong?
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Incorporating Variables in Xlookup Statements
Reply #1 - Feb 29th, 2004 at 3:17pm
Print Post Print Post  
Bharat,

I think it's a type conversion issue. You are passing an Int as a String argument. Try something like:

writeLn ("Value with Variable = " + @xlookupr (@fn, @Str(n), "Vital!Q2", "Date1"))

Let me know if that works better.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: Incorporating Variables in Xlookup Statements
Reply #2 - Feb 29th, 2004 at 4:49pm
Print Post Print Post  
Erika,

I tried that, it does not gets any value just like before.  The hard 9999999999999 is a number as it is not put within the " ",  and variable n is also a number. I believe, the problem might be related to how Integers are stroed in the system like 12.000000 for 12.  One can change the display by changing display format but that would not change how it is stored internally.

For me this is an academic issue, as I think, I am leaning more towards going different route.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Incorporating Variables in Xlookup Statements
Reply #3 - Feb 29th, 2004 at 4:55pm
Print Post Print Post  
Quote:
The hard 9999999999999 is a number as it is not put within the " "

This is not necessarily the case. SBasic tries very hard to convert what you give it to the correct type. It may automatically convert the hardcoded 99999999999999.
Quote:
I believe, the problem might be related to how Integers are stroed in the system like 12.000000 for 12.  One can change the display by changing display format but that would not change how it is stored internally.

That shouldn't be having an effect, but I'll take a look.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: Incorporating Variables in Xlookup Statements
Reply #4 - Feb 29th, 2004 at 5:11pm
Print Post Print Post  
Quote:
It may automatically convert the hardcoded 99999999999999.


We are dealing here with xlookupR statement, so the  appropriate format should be Number, I think. This is important issue because in loops, in order to go up and down, one has to be dealing with numbers and if we have issue with numbers, it will limit the use of x-family functions in loop to certain extent. Erika, thank you for offering to look into this.
  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Incorporating Variables in Xlookup Statements
Reply #5 - Mar 1st, 2004 at 3:29pm
Print Post Print Post  
Bharat,

I suspect that by using an "int" as opposed to a string variable, you are forcing SBasic to try to convert 999,999,999,999 to a 32-bit integer. The largest positive value a 32-bit integer can hold is 2,147,483,647. This causes the value in your "int" to wrap around into negative number land (around and around), and eventually lands at the value: -727,379,969 - which results in odd comparisons.

By "hard-coding" the 999,999,999,999 into the argument (as opposed to using the integer variable), you are avoiding the conversion to an integer. SBasic is smart enough to convert this directly into a string (the argument type for key in XLookUpR), and the actual search routine inside XLookupR converts it to a "double" (a 64-bit floating point value), before use.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: Incorporating Variables in Xlookup Statements
Reply #6 - Mar 1st, 2004 at 4:30pm
Print Post Print Post  
Quote:
The largest positive value a 32-bit integer can hold is 2,147,483,647. This causes the value in your "int" to wrap around into negative number land (around and around), and eventually lands at the value: -727,379,969 - which results in odd comparisons.


Mark,

Now it makes sense as to why it did not work. I have been making Unique sequential Numbers Using @num (@date) + 3 digits sequential number. So for a day it gives me 999 numbers which I can read and make sense.  The numbers will be 20040301001,  20040301002....etc. but that seems to be putting me over the Integer capacity. Well, I could be cutting down the first two digits and make 9 digits to bring it within the limit of Integer value, and since Y2K problem is behind us, I should be fine for another 96 years and that will perhaps out-live all of us.  Or I could settle for having just 99 unique sequential numbers for day and leave the year part untruncated and it will be good for another 110 years. What I was trying to do, was to locate the highest number and the counting down in the loop.

Eventhough I worked out an alternate method, this knowledge in the background will definitely help in the future. Thank you very much.
  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Incorporating Variables in Xlookup Statements
Reply #7 - Mar 1st, 2004 at 4:39pm
Print Post Print Post  
Bharat,

Instead of cutting down the size of your number, use a "double" when you need it to numeric, and use a string when passing it to "XLookUpR". That way you don't need to worry about it "turning over" anytime during the human occupation of this planet.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: Incorporating Variables in Xlookup Statements
Reply #8 - Mar 1st, 2004 at 4:58pm
Print Post Print Post  
Thanks Mark. I will remember that.
  
Back to top
 
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: Incorporating Variables in Xlookup Statements
Reply #9 - Mar 1st, 2004 at 7:35pm
Print Post Print Post  
Tried out loops and procedures with variable as Double and it works perfectly even with large numbers. Thanks Mark.
  
Back to top
 
IP Logged