Jump to content
Sign in to follow this  
dRsrb

Array and strings; If statement

Recommended Posts

dRsrb

Hello! :o

I have two general problems which I don't understand:

#include <array.au3>

_1_Array_Problem()
_2_If_Problem()

Func _1_Array_Problem()
    
    Local $aTest[3]
    
    $aTest[0] = 'Test'
    $aTest[1] = 'Test|String'
    $aTest[2] = 'String'

    _ArrayDisplay($aTest)
    
EndFunc

Func _2_If_Problem()
    
    Local $vTest
    
    If $vTest = '' Then MsgBox(0, '1: $vTest is empty', $vTest)
    If $vTest == '' Then MsgBox(0, '2: $vTest is empty', $vTest)
    
    $vTest = 0
    
    If $vTest = '' Then MsgBox(0, '3: $vTest is empty', $vTest)
    If $vTest == '' Then MsgBox(0, '4: $vTest is empty', $vTest)
    
    $vTest = ''
    
    If $vTest = '' Then MsgBox(0, '5: $vTest is empty', $vTest)
    If $vTest == '' Then MsgBox(0, '6: $vTest is empty', $vTest)
    
EndFunc

1.

Why is everything behind the '|'-char cropped? The '|'-char is part of the string, so I don't understand this behavior. Could it be a possible bug or is there a way to escape the vertical line? 'Chr(124)' doesn't work as well.

2.

Firstly I used '==' in statements until I noticed, that this method doesn't work with MsgBox(). So I started to use in general only one '=' in my script to not mix them all the time.

Today I came across this behavior: If '$vTest' is '0' then 'If $vTest = '' Then ...' evaluates to true. But '$vTest' is zero and not empty. Now I'm forced to mix '==' and '='.

And I'm confused. Why does this not work as expected? Why are there two methods at all? :D

Bye :D

Edited by dRsrb

Share this post


Link to post
Share on other sites
bo8ster

dRsrb

Some good questions

1. I tried using escape characters like \| and ||. Searching the forum I can only suggest looking here..

I also wondering if anyone else can get it going. I found nothing in the help file not much searching. I did find this. Any suggestions?

2. I have seen this around. I would say, just because AutoIt allows you to be a little lazy, don't be. I would tread == and = like you would in C. Running your program shows me that '==' holds true in all situations. With the first test, I would recommend you initialize that var before you use it like any good programmer. This will stop you having unexpected bugs.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites
exodius

The first situation only happens because of _ArrayDisplay displaying things as a ListView (which uses the "|" to delimit column info).

If you do:

MsgBox (0, "", $aTest[1])

after you define $aTest[1] then it works just fine.

Edited by exodius

Share this post


Link to post
Share on other sites
Ascend4nt

$vTest = 0

If $vTest = '' Then MsgBox(0, '3: $vTest is empty', $vTest)

If $vTest == '' Then MsgBox(0, '4: $vTest is empty', $vTest)

Valik recently enlightened me that '==' *forces* a string comparison (and makes it case-sensitive). But the whole bit of '' equals 0 confuses me (unless '' basically means a null pointer, but how it would work in an actual string comparison I wouldn't have a clue).

A test for 'If Not $vTest' would make more sense for my code...

Edited by ascendant

Share this post


Link to post
Share on other sites
bo8ster

That would make sense. C/C++ == usually goes for the memory address and you have to use a string compare method to compare strings.

Ideally you would get a compile error as you are comparing two different data types, as you say 0 could be seen as null. Generally NULL = 0.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites
Ascend4nt

Actually, in C/C++, == is just a regular 'is equals' operation. [and can't be used to compare strings]

'=' in C/C++ on the other hand is an assignment operator.

Regarding the == string comparison, I just realized that its pretty much the same as doing:

StringCompare($vTest,0)

0 is converted to '0', so null or an empty string won't equal it, which is why this results in True (when $vTest=0):

If $vTest == '0'

Edited by ascendant

Share this post


Link to post
Share on other sites
bo8ster

That's correct. It compares either the pointers or the first element in the array, if forget which, because a string var is a pointer to the start of the array.

Now if 0 is converted to a char, that makes things interesting. I would expect "Test = 0 - if Test == 0" to be true every time. That said, in my scripts you will often see int() in use.

Either way in summary I recommend using == not = for testing equality.

http://en.wikipedia.org/wiki/Equality_(relational_operator) holds some reasons why = is allowed.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites
Ascend4nt

The reason = is allowed is totally up to the language interpreter/compiler, it doesn't matter what Wikipedia says. However, it can make debugging easier if you can clearly see where the assignments vs. comparisons are - BUT there's a problem in using '==' in AutoIt, as it incurs a performance hit as Valik pointed out.

Share this post


Link to post
Share on other sites
bo8ster

Thanks for the link ascendant - I learnt something here.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites
dRsrb

I learnt something here.

Me too! Absolutely important tips!

Thank you all very much! :D

Share this post


Link to post
Share on other sites
andybiochem

Hello! :D

I have two general problems which I don't understand:

.......

1.

Why is everything behind the '|'-char cropped? The '|'-char is part of the string, so I don't understand this behavior. Could it be a possible bug or is there a way to escape the vertical line? 'Chr(124)' doesn't work as well.

The _ArrayDisplay documentation shows how to get around this:

#include <array.au3>

_1_Array_Problem()

Func _1_Array_Problem()
    
    Local $aTest[3]
    
    $aTest[0] = 'Test'
    $aTest[1] = 'Test|String'
    $aTest[2] = 'String'

    _ArrayDisplay($aTest,"",-1,0,"¬")
    
EndFunc

- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites
dRsrb

The _ArrayDisplay documentation shows how to get around this: ...

Thank you for the hint. I just edited the "Array.au3" and replaced the vertical line with an other default char.

Bye :D

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  

×