Page 1 of 1

function returns string with quotes

Posted: Thu Dec 14, 2017 5:46 pm
by yeldarb
Probably something really simple...

So I've got a function that simply returns a string "high", "low" and "average" based on the value passed to it.

Code: Select all

put getLevel (theValue) into theLevel

function getLevel aValue
   if aValue < 45 then
      return 'low'
   else if aValue > 55 then
      return 'high'
   else
      return 'average'
   end if
end getLevel
But when I try to concat the value returned to another string it includes the single quotes:

Code: Select all

put findText & "-" & theLevel
> $conscLevel0-'average'

Re: function returns string with quotes

Posted: Thu Dec 14, 2017 6:01 pm
by yeldarb
Ok, I see I need to study the difference between single and double quotes... doubles work fine, singles get passed for some reason.

Re: function returns string with quotes

Posted: Thu Dec 14, 2017 6:08 pm
by Klaus
Hi yeldarb,

1. welcome to the forum!

2. A string is everything in QUOTES, but this means DOUBLE quotes!
If you -> return 'a value in single quotes'
then the single quotes are seen by the engine as PART of the string you are returning!
Try yourself by: answer getlevel(10)

So in your case here, stick to double quotes! :D


Best

Klaus

Re: function returns string with quotes

Posted: Thu Dec 14, 2017 6:44 pm
by Klaus
Addition:
If your returned values consists of more than one word, this will fail completely!
So double quotes are mandatory, if you want to return a string!

This will put FALSE into the message box, not what you might exspect :)

Code: Select all

on mouseUp
   put getLevel (10) 
end mouseUp

function getLevel aValue
   if aValue < 45 then
      return 'low and high'
   else if aValue > 55 then
      return 'high and low'
   else
      return 'average to say the least'
   end if
end getLevel

Re: function returns string with quotes

Posted: Thu Dec 14, 2017 8:11 pm
by dunbarx
Hi.

What Klaus said.

Looking at it from the other side, know that double quotes are "native" elements in LC. But there is a constant called "quote". So the way to create something like (32") is:

Code: Select all

answer "32" & quote
Here you can see both "types" of quote.

Craig Newman

Re: function returns string with quotes

Posted: Fri Dec 15, 2017 5:54 am
by bogs
Or, of course, you could save almost no typing and do this :P
Image
Just kidding, all of the above is correct.

Re: function returns string with quotes

Posted: Tue Dec 19, 2017 5:16 pm
by yeldarb
Related question--I have five buttons, and for the sake of simplicity I have named them "1", "2", "3", "4", and "5" because it's a rating system and I can just put the name of the btn into the fld. But when I reference them with a loop:

Code: Select all

repeat with i = 1 to 5
      set the icon of btn i to the id of image (i & "-unchecked")
end repeat
LC treats the var i as a number, and refers to the btn as it's ordered on the page. How do I tell LC to treat the number as a string (as in i.toString() or String(i))?

Re: function returns string with quotes

Posted: Tue Dec 19, 2017 5:23 pm
by Klaus
Hi yeldarb,

you can't!

The engine will refer to a NUMBER as a name of an object as its LAYER on the card:
So -> button "1" will be treated ad the FIRST button on the card, even if you have it crreated
on top of all other objects on the card.

Name your buttons something like "b1", "b2" etc. so you can still access them easily e.g. in a repeat loop.


Best

Klaus

Re: function returns string with quotes

Posted: Tue Dec 19, 2017 5:30 pm
by dunbarx
Do you see what Klaus meant? You would name them as he mentioned and then modify one line in your handler:

Code: Select all

  set the icon of btn ("b" & i) to the id of image (i & "-unchecked")
This is standard practice, and your brain will "see" the concatenated string ("b" & i) in the same way you originally thought, but now LC's internal ordering of object references is out of play.

Craig Newman

Re: function returns string with quotes

Posted: Tue Dec 19, 2017 5:54 pm
by yeldarb
Thanks, it's a bit of a challenge coming from languages with type casting. Is there any way to force casting in LC?

Re: function returns string with quotes

Posted: Tue Dec 19, 2017 6:10 pm
by FourthWorld
LiveCode is generally typeless; type needs are identified and coerced on the fly.

What do you want to cast?

Re: function returns string with quotes

Posted: Tue Dec 19, 2017 7:29 pm
by SparkOut
Really, you should never name any object with just a number. An object has various properties, one of which is the name, another of which is the number, and they reference the object differently. If you refer to an object such as field "1" this will not be interpreted as the name, but as field number 1, which may reference a completely different object. The way the engine works, I don't think there is any way that you can get a casting technique to override this interpretation.

Re: function returns string with quotes

Posted: Tue Dec 19, 2017 8:23 pm
by jiml
In addition to NAME and NUMBER Livecode objects are also assigned an ID, which is yet another way to reference them.

Jim Lambert

Re: function returns string with quotes

Posted: Tue Dec 19, 2017 11:28 pm
by dunbarx
And an altID.

Craig