Trouble with string comparison

Discuss your code, get questions answered

Trouble with string comparison

Postby stevey93 » Sun Jul 15, 2012 8:33 am

I seem to be having trouble comparing strings in one of my functions. The function, 'h(x, str),' takes two values, the first a number and the second a string. The following is the code for 'h':
Code: Select all
function (x, str) {
   if (str == "One")
      return x+1;
   if (str == "Two")
      return x+2;
   ...
   if (str == "Ten")
      return x+10;
   return 0;
}
Every time I call 'h,' it seems to fail each condition for all of the if-statements, going straight to 'return 0;' e.g. if I call h(1,"One") it will return '0' instead of '2.' I've figured out a workaround which involves creating the following global variables: str1 = "One" str2 = "Two" ... str10 = "Ten" and modifying the code of 'h' to the following:
Code: Select all
function (x, str) {
   if (str == str1)
      return x+1;
   if (str == str2)
      return x+2;
   ...
   if (str == str10)
      return x+10;
   return 0;
}
The problem with this is that I don't want to have all these global variables: str1, ... str10, cluttering up the folder that they are contained in. Additionally, if I try to avoid this problem by declaring and initializing str1, ...str10 to the appropriate values within the function itself, it behaves exactly the same as it did in the first place, returning '0' every time. Any help on this issue would be greatly appreciated.

Thanks,
Stephen
stevey93
 
Posts: 6
Joined: Sun Jul 08, 2012 1:47 pm

Re: Trouble with string comparison

Postby oliver » Sun Jul 15, 2012 1:29 pm

Stephen,

Ah. Your problem is actually not a string comparison problem. The problem is that a String in ND1 isn't exactly a normal string in JavaScript. (Numbers and arrays are. Other objects don't easily map to JS. For example, a complex number is (currently) a string in JavaScript. A function like complex.sin(x) takes a complex number and returns one, but, really, the object processed is a string. That's because there're no native Complex number objects in JS. Other ND1 data types, like BigInt, are proper JS objects.)

A string "One" looks like this '"One"' (hard to see; that's ' " One " ' (w/o spaces)) in JavaScript. That is, the double-quotes are part of the string!

So, to fix your comparisons, just wrap each literal compare string in single quotes.

Or, preceding your existing code, convert each incoming string into a JS string like so
Code: Select all
str = string.stripFirmDelimiters(str);

or so
Code: Select all
str = calculator.unquote(str);


Hope this helps.

Oliver
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Re: Trouble with string comparison

Postby stevey93 » Sun Jul 15, 2012 2:36 pm

Thank you Oliver,

I added str = calculator.unquote(str); to the top of 'h' and it now returns the correct value, 2, when I call 'h(1, "One")'. I am however running into just one additional problem. As you can probably tell, the version of 'h' that I posted is a simplification of a more useful function that I would like to be able to graph. This is where I start running into more trouble. When I store 'h(x, "One")' to 'eq' and press 'draw,' it shows 'h' as taking the value '0' for all values of 'x.' If you find a way to get it to graph correctly, please let me know.

Also somewhat related, I have a different function 'f(x, y)' which takes in two numeric values and returns a numeric value. If I try storing 'f(x, 0) = g(x)' to 'eq' it gives a parse error. If I instead reverse the order to 'g(x) = f(x, 0)' and store it to 'eq,' it stores correctly. I realize this is a very minor issue, but it still would be nice to store these in any order.

Thanks again,
Stephen
stevey93
 
Posts: 6
Joined: Sun Jul 08, 2012 1:47 pm

Re: Trouble with string comparison

Postby oliver » Sun Jul 15, 2012 4:43 pm

Hi Stephen,

Not sure, we're seeing the same but please try this:
- create a variable 'j' with contents 'h(x, "One")'
- store 'j' into 'eq'

Yet another work-around would be to store << "One" h >> in 'eq' and plot that. (This is slower, though.)

I'm getting a "@draw: Can't find variable: One" error when I try to plot 'h(x, "One")'. I will investigate.

I can confirm the problem with storing 'f(x, 0) = g(x)' in 'eq' and will investigate that, too.

Thanks for reporting.

Oliver
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Re: Trouble with string comparison

Postby stevey93 » Sun Jul 15, 2012 6:17 pm

Hi Oliver,

I did try both workarounds and was able to get the first one to work. Unfortunately this sort of defeats the purpose of having any if-statements in 'h' when instead I could just have each 'return' statement in their own functions j, k, l,... and store them to 'eq' when needed. I'm also still looking for some more workarounds, but have only had luck with commenting out the individual if-statement that I want the graph of. That is, if I want to graph 'h(x, "Two")' I would change its code to

Code: Select all
function (x, str) {
   str = calculator.unquote(str);
   if (str == "One")
      return x+1;
   //if (str == "Two")
      return x+2;
   ...
   if (str == "Ten")
      return x+10;
   return 0;
}

Of course, this can get quite annoying if, for example, I want to switch from graphing "Two" to graphing "Three" and then to "One" which is why I'm hoping a better fix can be found. Also, I will occasionally get a "@draw: Can't find variable: One" error too, but other times it will go through without an error (it still won't display correctly though). My guess (I haven't completely confirmed this) is that my actual function takes in the names of other functions within the same folder. That is, it'd be like storing 'h(x, "One")' into 'eq' when "One" is the name of an existing variable within that folder.

Stephen
stevey93
 
Posts: 6
Joined: Sun Jul 08, 2012 1:47 pm

Re: Trouble with string comparison

Postby oliver » Mon Jul 16, 2012 2:39 am

Stephen,

Having looked internally, it seems I broke something recently when doing an optimization. This currently precludes functions that take multiple parameters from working in expressions that will be graphed.
The suggested work-around defeats this optimization mechanism. Instead of hard-coding "One" in j, you could have a variable that you set elsewhere. That is, j would contain 'h(1, method)', where "method" is a folder variable containing the currently selected graphing method.

How did you plan on passing the number string into "h()"? If setting the number to graph could be part of your graphing set up, maybe the "method" suggestion can work for you. Of course, if you have "method", you don't need "j" anymore if it's ok to reduce h to a function that takes one parameter, and switches inside on "method". (In JS, btw, you can use strings in a switch-statement.)

The upcoming UI-building update will let you display an iOS picker to the user to have them pick an entry from an array of choices. That could be used to set your folder's "method" variable, and provide a decent interface for selecting what to graph.

In the meantime, you could use INPUT to have the user type his choice in (not so convenient, as typing is required), or, if you only have a few choices, provide a button that will set the method with code like this << "One" 'method' @var_store >> (<< "One" 'method' STO >>).

Oliver
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Re: Trouble with string comparison

Postby stevey93 » Mon Jul 16, 2012 4:36 pm

Thank you Oliver,

I've gone with the option of making a button which assigns "One" to 'method' & changed 'h' to only take in 'x' and this works perfectly since I can't really imagine anyone using 'h' outside of its folder

Cheers,
Stephen
stevey93
 
Posts: 6
Joined: Sun Jul 08, 2012 1:47 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron