Sign in to follow this  
Followers 0
Zohar

[Closed] How Can I do 2 Operations in a While Condition?

15 posts in this topic

#1 ·  Posted (edited)

Hi

I need to do 2 operations in a While's Condition.

The 2 operations are:

1) Setting a Variable

2) Conditioning, according to the Content of the Variable that was just assigned

I need something like this:

While ( $Data=SomeFunctionForGettingTheData() ) = ""
     Operations
Wend

In many programming Languages,

the While condition that I just wrote, will do 2 operations:

1) It will do the assinment $Data=SomeFunctionForGettingTheData()

2) It will check the properly check if the value assigned, equals "",

And the reason it will do it, because an assignment's return value, is the value that was assigned.

(so like, the value of a codepiece like that $V=5, will be 5)

How can I make the same in AutoIt?

Thank you

Edited by Zohar

Share this post


Link to post
Share on other sites



Zohar,

I would use a Do...Until loop like this: ;)

Global $sData

Do
    $sData = InputBox("Data Input", "Input here")
Until $sData <> ""

MsgBox(0, "Data Assigned", $sData)

That way you both assign and check the value in the loop. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi Melba23 :)

Thank you,

The only problem is that we have the Operations part.

I need both the Assignment and the Condition to be checked before the Operations block is performed.

What can we do in this case?

I am ooking for a way to write the While's Condition, like in C++,

Where you can write an Assignment to a variable,

and that assignment's value, will also be the value of the whole clause..

Is it somehow possible?

Edited by Zohar

Share this post


Link to post
Share on other sites

Zohar,

This perhaps: :huh:

Global $sData

HotKeySet("{ESC}", "On_Exit")

While 1

    ; Assign and check
    Do
        $sData = InputBox("Data Input", "Input here")
    Until $sData <> ""

    ; Operations
    MsgBox(0, "Data Assigned", $sData)

WEnd

Func On_Exit()
    Exit
EndFunc

But it is difficult to be helpful when I have so little idea of what exactly you are trying to do. There might well be another method more suited but without a better explanation it is impossible to say. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Right now, my workaround is this:

Local $Data

While True
    $Data =SomeFunctionForGettingTheData()
    If $Data<>"" Then ExitLoop

    ;Operations
    Sleep(1000)
WEnd

However,

If I could have the Assignment and the Condition in the While's Condition clause,

the block could be like this:

While ($Data =SomeFunctionForGettingTheData()) = ""
    Beep(1000,10)
    Sleep(1000)
WEnd

If only an Assignment in AutoIt, returned the Value that was assigned, the whole problem would've been solved..

Regarding your question,

since this problem occured to me in many places,

then there's not just 1 scenario - it's a basic coding problem, it has nothing to do with what you try to achieve in your program..

Edited by Zohar

Share this post


Link to post
Share on other sites

Zohar,

If I could have the Assignment and the Condition in the While's Condition clause

But I do not believe you can. And all you would achieve is more complicated, but slightly shorter, code - why not KISS? ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

OK then.

Thank you Melba..

Share this post


Link to post
Share on other sites

I personally would go with:

...
$Data = SomeFunctionForGettingTheData() ;; first/initiation.
While $Data = ""
    ;Operations
    Sleep(1000)
    $Data = SomeFunctionForGettingTheData() ;; next/continuation
WEnd
...

Although, I figure this is also possible.

...
Dim $Data
While IsEmptyString($Data, SomeFunctionForGettingTheData())
    ;Operations
    Sleep(1000)
WEnd
...
Func IsEmptyString(ByRef $Data_out, $Data_in)
    $Data_out = $Data_in
    Return ($Data_out = "")
EndFunc

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

I personally would go with:

...
$Data = SomeFunctionForGettingTheData() ;; first/initiation.
While $Data = ""
;Operations
Sleep(1000)
$Data = SomeFunctionForGettingTheData() ;; next/continuation
WEnd
...

The only problem with it, is that you repeat the $Data = SomeFunctionForGettingTheData() line.

Although, I figure this is also possible.

...
Dim $Data
While IsEmptyString($Data, SomeFunctionForGettingTheData())
;Operations
Sleep(1000)
WEnd
...
Func IsEmptyString(ByRef $Data_out, $Data_in)
$Data_out = $Data_in
Return ($Data_out = "")
EndFunc

This one is interesting :)

Thanks

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Zohar,

The only problem with it, is that you repeat the $Data = SomeFunctionForGettingTheData() line.

See your "I need something like this:" example of your first post:

While ( $Data=SomeFunctionForGettingTheData() ) = ""
Operations
Wend

Since the condition has to be checked every loop count the "$Data=SomeFunctionForGettingTheData()" part in the while condition will be repeated every loop count also. So it doesn't matter if it is done in the while-condition or within the loop.

Or did I missunderstand you?

A-Jay

Edited by ajag

Rule #1: Always do a backup         Rule #2: Always do a backup (backup of rule #1)

Share this post


Link to post
Share on other sites

The problem is not that the "$Data=SomeFunctionForGettingTheData()" will run every iteration - it needs to run every iteration.

The problem is in the fact that the discussed idea, contains code duplication, which should be avoided, for many reasons.

Share this post


Link to post
Share on other sites

The problem is in the fact that the discussed idea, contains code duplication, which should be avoided, for many reasons.

No it shouldn't, if you need to check it before the While loop, check it before the While loop. There's nothing wrong with that scenario. It would be the same if you were using a timer in a While loop, you would set the TimerInit() before the While loop, and then reset it with the same line inside the loop.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

BrewManNH,

I completely disagere with you on this one.

But you can code however way you prefer.

I code the way I prefer.

If AutoIt made Assignment Statements return the value of what was just assigned (like it happens in all other programming langauges),

then the problem would have not existed.

But from the comments here, it seems that there's no way to solve it.

So I used the workaround I mentioned.

Edited by Zohar

Share this post


Link to post
Share on other sites

If AutoIt made Assignment Statements return the value of what was just assigned (like it happens in all other programming langauges),

then the problem would have not existed.

Not ALL other programming languages, Python for instance doesn't return a value on an assignment. But that's neither here nor there, AutoIt doesn't, and workarounds can be done as you say.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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