function returns string with quotes

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
yeldarb
Posts: 43
Joined: Thu Dec 14, 2017 5:39 pm

function returns string with quotes

Post by yeldarb » Thu Dec 14, 2017 5:46 pm

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'

yeldarb
Posts: 43
Joined: Thu Dec 14, 2017 5:39 pm

Re: function returns string with quotes

Post by yeldarb » Thu Dec 14, 2017 6:01 pm

Ok, I see I need to study the difference between single and double quotes... doubles work fine, singles get passed for some reason.

Klaus
Posts: 13828
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: function returns string with quotes

Post by Klaus » Thu Dec 14, 2017 6:08 pm

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

Klaus
Posts: 13828
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: function returns string with quotes

Post by Klaus » Thu Dec 14, 2017 6:44 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9660
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: function returns string with quotes

Post by dunbarx » Thu Dec 14, 2017 8:11 pm

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

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: function returns string with quotes

Post by bogs » Fri Dec 15, 2017 5:54 am

Or, of course, you could save almost no typing and do this :P
Image
Just kidding, all of the above is correct.
Image

yeldarb
Posts: 43
Joined: Thu Dec 14, 2017 5:39 pm

Re: function returns string with quotes

Post by yeldarb » Tue Dec 19, 2017 5:16 pm

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))?

Klaus
Posts: 13828
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: function returns string with quotes

Post by Klaus » Tue Dec 19, 2017 5:23 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9660
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: function returns string with quotes

Post by dunbarx » Tue Dec 19, 2017 5:30 pm

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

yeldarb
Posts: 43
Joined: Thu Dec 14, 2017 5:39 pm

Re: function returns string with quotes

Post by yeldarb » Tue Dec 19, 2017 5:54 pm

Thanks, it's a bit of a challenge coming from languages with type casting. Is there any way to force casting in LC?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9834
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: function returns string with quotes

Post by FourthWorld » Tue Dec 19, 2017 6:10 pm

LiveCode is generally typeless; type needs are identified and coerced on the fly.

What do you want to cast?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: function returns string with quotes

Post by SparkOut » Tue Dec 19, 2017 7:29 pm

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.

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: function returns string with quotes

Post by jiml » Tue Dec 19, 2017 8:23 pm

In addition to NAME and NUMBER Livecode objects are also assigned an ID, which is yet another way to reference them.

Jim Lambert

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9660
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: function returns string with quotes

Post by dunbarx » Tue Dec 19, 2017 11:28 pm

And an altID.

Craig

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”