string if statement

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times

string if statement

Post by achillis1 »

Hello,

I create a string of default value of 20.
Then I want to have a desicion icon that when the string has a certain value then folow the ''yes'' route. But when I state if; string[] = 33 then it shows error. It wants to state as if string[x] =33.


Also if I read the USB serial port (read string) and have the input data(e.g hello) be assigned to a string named Mystring; then how do I say to flowcode the following: if Mystring = hello > do that or .....

Thank you,

Best Regards,

Andreas Achilleos

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: string if statement

Post by Benj »

Hello Andreas,

You can use the compare function in the string manipulation icon.

test = Compare$(string, "33", 1)

if the strings are equal then test will equal 0.

Here is the Flowcode help file regarding the compare function.

Compare$(string1, string2, compare_type)
Compares the two strings, parameters 1 and 2, and returns a BYTE value corresponding to the following results:

0 = strings are identical
1 = string1>string2
255 = string2>string1

The 3rd parameter, compare_type, determines whether or not the check is case sensitive. values for compare_type are:

0 = case sensitive
1 = case insensitive.
Examples

Str1 = "ABC"
Str2 = "abc"

RetVal = Compare$(Str1, Str2, 0)

RetVal is now 255 as Str2 is later in the ASCII sequence.

RetVal = Compare$(Str1, Str2, 1)

RetVal is now 0 as when case insensitive the two strings are identical.

Str2 = Str1
RetVal = Compare$(Str1, Str2, 0)

RetVal is now 0 as both strings are identical.

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times

Re: string if statement

Post by achillis1 »

Thanks Ben!!!

I've got another one: If I want to store a single byte value e.g 155 to a string then I will have to create a string of 1 caharacter long i.e string[1]?

Then if I want to store the word '' hello '' I would create a string of 5 characters i.e string[4]?
If I say Mystring[20] = 255 or MYstring[20]= '' yes'' then what array characters are occupied and the remaining what values do they got?
Regarding the example Mystring = ''yes'' I do this in the string manipulaton icon and says that I have an undefined variable and if I state Mystring = 4 then it says that I have a wrong type variable.

Thank you

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: string if statement

Post by Benj »

Hello,

A string is quite simply an array of bytes.

So a string of 20 characters is actually 20 bytes numbered 0 - 19.

eg

string[0] = x
...
string[19] = y

To store Hello you would need a string[5] eg 5 characters.
string = "hello"

string[0] = 255 loads the byte value 255 into the first byte of the string.

string[0] = "Yes" This will not work and is not recommended

string[0] = 'Y'
string[1] = 'e'
string[2] = 's' is permitted

or

string = "yes"

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times

Re: string if statement

Post by achillis1 »

Thanks Ben,

About the string = ''yes''
I do this in the strring manipulation icon and does not accept it! It says that the line contains an undefined variable and means the ''yes''

Frank123
Posts: 36
Joined: Fri Jan 15, 2010 5:17 pm
Location: Saint-Nazaire - France
Has thanked: 1 time

Re: string if statement

Post by Frank123 »

I have same problem.
Frank
Electronics teacher

Frank123
Posts: 36
Joined: Fri Jan 15, 2010 5:17 pm
Location: Saint-Nazaire - France
Has thanked: 1 time

Re: string if statement

Post by Frank123 »

Ok i didn't use S= icon.
Frank
Electronics teacher

Frank123
Posts: 36
Joined: Fri Jan 15, 2010 5:17 pm
Location: Saint-Nazaire - France
Has thanked: 1 time

Re: string if statement

Post by Frank123 »

if i send a string >127 char to LCD flowcode crash.
Frank
Electronics teacher

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: string if statement

Post by medelec35 »

achillis1 wrote:Thanks Ben,

About the string = ''yes''
I do this in the strring manipulation icon and does not accept it! It says that the line contains an undefined variable and means the ''yes''
You would get that message if you placed String="Yes" in a calculation box.
You need to place String="Yes" within a string manipulation box e.g:
String_Yes.jpg
String_Yes.jpg (7.88 KiB) Viewed 8772 times
Martin
Martin

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 91 times
Been thanked: 8 times

Re: string if statement

Post by achillis1 »

OK, the problem was that the punctuation marks I used were created by the wrong key!
If you see in the first screenshot the space between the punctuation is bigger than the space in the second screenshot!

So if anybody else is having the same problem then it would be best to start looking for the right key! On my keyboard the right quotation marks is generated by shift+2.

Thank you.
Attachments
matrix.jpg
matrix.jpg (38.37 KiB) Viewed 8768 times
matrix2.png
(3 KiB) Downloaded 4802 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: string if statement

Post by Benj »

Hello,

The single quotation is used to denote a character.

For example using a calculation icon you can do this.

bytevar = 'a'
bytevar = bytevar - '0'

The double quotation is used to denote a string.

For example in the string manipulation icon you can do this.

string = "hello" + " world"
string = string + "..."

Post Reply