Sign in to follow this  
Followers 0
4got

compare a variable to a string

13 posts in this topic

HI to everybody..I'm new in this forum,my nick is 4got, I'm not english so please forgave me if in my post you find some errors...By the way I have a problem when I compare a variable and a string.This is the code:

$Data = TCPReceive($ConnectedSocket) ;move the mesage in the variable $Data

$compare=StringCompare($Data,"start",0) ;compare the string with the mesage

If $compare=0 Then ;condition

MsgBox(0,"Message","The message is start")

ElseIf

$Data >=0 then

MsgBox(0, "Message", $Data)

EndIf

If someone could help me... thx

Share this post


Link to post
Share on other sites



This has to be on one line:

ElseIf

$Data >=0 then

ElseIf $Data >=0 then

Share this post


Link to post
Share on other sites

This has to be on one line:

ElseIf

$Data >=0 then

ElseIf $Data >=0 then

It still doesn't work.. It always executes the second if

ElseIf $Data >=0 then

MsgBox(0, "Message", $Data)

EndIf

I need that this program recognizes the command that I send. E.G if I send the command start the program will execute a certain operation and if I send another command it will do another operation..

I hope to be clear..

Share this post


Link to post
Share on other sites

It still doesn't work.. It always executes the second if

ElseIf $Data >=0 then

MsgBox(0, "Message", $Data)

EndIf

I need that this program recognizes the command that I send. E.G if I send the command start the program will execute a certain operation and if I send another command it will do another operation..

I hope to be clear..

What does the MsgBox display for the string $Data?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

What does the MsgBox display for the string $Data?

If I write "start" the MsgBox display "start".The compare doesn't work but the string is correct.

Share this post


Link to post
Share on other sites

If $Data = "start" Then

Share this post


Link to post
Share on other sites

If I write "start" the MsgBox display "start".The compare doesn't work but the string is correct.

Can't see why there is a problem. See what happens if you try this then

$Data = TCPReceive($ConnectedSocket);move the mesage in the variable $Data
$compare=StringInStr($Data,"start",0);test the string is in the message
If $compare>0 Then;condition
   MsgBox(0,"Message","The message is start")
ElseIf
   $Data >=0 then
   MsgBox(0, "Message", $Data)
EndIf

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Now the program works but I have other 2 problems:

-if I want to use a switch case can I put a string instead of a value?

switch($Data)
     case "start"
         ....

I tried this way but it didn't work..How can I do?

-if I don't wanna see the icon of the autoit application when it runs in the application bar of windows what I have to do?

Thx a lot to everybody who has answered to this post.

Share this post


Link to post
Share on other sites

Nobody could help me??

Share this post


Link to post
Share on other sites

should work fine ... what is the exact content of the variable $data ?

Application bar ? do you mean systemtray ? (#NoTrayIcon)

:)


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

should work fine ... what is the exact content of the variable $data ?

Application bar ? do you mean systemtray ? (#NoTrayIcon)

:)

In the variable $Data I put the message that is sent by means of the client program, in this case I've tried with the message "start" but it didn't work...

Yes the systemtray...sorry...

Share this post


Link to post
Share on other sites

Now the program works but I have other 2 problems:

-if I want to use a switch case can I put a string instead of a value?

switch($Data)
     case "start"
         ....

I tried this way but it didn't work..How can I do?

-if I don't wanna see the icon of the autoit application when it runs in the application bar of windows what I have to do?

It shouldn't work based on what you found earlier because your original problem was that $Data was not equal to "start" for some reason and this was cured by using

$compare=StringInStr($Data,"start",0);possibly some whitespace character in $Data

It would probably have been better to find out what was in $data that was causing the problem and strip it out, try using $Data = StringStripWS($Data,8).

Failing that, instead of switch use Select-

Select
 Case StringInStr($Data,"start",0) > 0
  ;something
.
.
.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

It shouldn't work based on what you found earlier because your original problem was that $Data was not equal to "start" for some reason and this was cured by using

$compare=StringInStr($Data,"start",0);possibly some whitespace character in $Data

It would probably have been better to find out what was in $data that was causing the problem and strip it out, try using $Data = StringStripWS($Data,8).

Failing that, instead of switch use Select-

Select
 Case StringInStr($Data,"start",0) > 0
 ;something
.
.
.
yes now it's perfect...when it will be complete I'll post it..thx to everybody

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0