Jump to content

(solved) Obtain value from array returned by function


Recommended Posts

So I am creating a script where a user enters values into an input box and the values are delimited by commas.  Normally when I am working with the select statements which indicate code to run depending on user action, I like to customize the portion of the select condition used to determine valid input and add custom conditions to it as to make it valid within the context of what I am trying to accomplish.  Normally I do this by comparing a called function to a return value from the called function, since the select statement has to occur right after the input box declaration.  However, in this case, I plan to use the StringSplit function to obtain the individual values and remove the delimiters.  However, since this function returns an array, the only way that I am aware to access the values returned by the function is assigning a variable to the function call and then reading a value from a specified index.  However, that would prevent me from adding my personal conditional statements to the valid input select condition, since I have declare the variable.  Is there a way to access the values in an array returned by a function without first declaring it as a variable, or will I just have to specify my own "valid" conditions from within the valid input select block?  To paint a clearer picture of what I am asking, here is visual example of what I am trying to do:

$sInputBoxAnswer = InputBox("Title text","Text goes here which asks user to input delimited values in the input",""," M")
Select
    Case @error = 0 And StringIsDigit(StringSplit($sInputBoxAnswer, ",")[1]) = 1
    Valid code executes here.
    Case @Error = 1 ;The Cancel button was pushed
        Exit
    Case Else ;Any other action occurs or input is received that is not defined above
        Exit
EndSelect

 

Edited by MattHiggs
Link to comment
Share on other sites

30 minutes ago, alien4u said:
30 minutes ago, alien4u said:

I still can't get what you want to do...

What is this:

StringIsDigit(StringInString($sInputBoxAnswer, ",")[1]) = 1

What is StringInString?

Anyways take a look to _StringExplode()

Regards
Alien.

I still can't get what you want to do...

What is this:

StringIsDigit(StringInString($sInputBoxAnswer, ",")[1]) = 1

What is StringInString?

Anyways take a look to _StringExplode()

Regards
Alien.

Hello Alien, and thank you for your response.  I have updated the example I provided.  It should have been StringSplit.  Let me see if I can explain this any better.   In the above example, the particular line that you selected is exactly what I want you to look at, but not what you pointed out.  As you probably know, the StringSplit function returns an array, and I would like to be able to compare the values stored in the returned array to what my script will indicate as a "valid response", but as far as I know, you need to declare a variable equal to the array returned by the function in order to access the elements, but if I do this, I can't include it in the select statement condition which runs when a valid input is entered.  The line that you picked out is "visual representation" of what I am trying to accomplish (change StringinString to StringSplit): If you look closely, within the StringIsDigit, I have called the the Stringsplit function, and right after the closing parentheses for stringsplit, I wrote "[1]", as in, pass the value of Stingsplit at the index 1 in the returned array to the StringisDigit function.  Make more sense?

Edited by MattHiggs
Link to comment
Share on other sites

in that way you can check only one element of the array, the element [1] in this case, while all other elements are simply  ignored. If you need to check each element of the array, (that is, each comma separated value inserted by user) then this way is not suitable.

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

oh, sorry... well, that kind of syntax to directly get single elements "on the fly" from a returned array, without the need of storing it on a variable, is an allowed syntax by AutoIt.

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

IMHO the error checking is redundant and could be done simpler like this

$sInputBoxAnswer = InputBox("Title text","Text goes here which asks user to input delimited values in the input",""," M")

$aSplit = StringSplit($sInputBoxAnswer, ",")
; this checks all required error cases :
If not StringIsDigit($aSplit[1]) Then Exit Msgbox(0,"", "failure")
;Valid code executes here (allows the direct further use of $aSplit)
Msgbox(0,"", "ok")

 

Link to comment
Share on other sites

4 hours ago, mikell said:

IMHO the error checking is redundant and could be done simpler like this

$sInputBoxAnswer = InputBox("Title text","Text goes here which asks user to input delimited values in the input",""," M")

$aSplit = StringSplit($sInputBoxAnswer, ",")
; this checks all required error cases :
If not StringIsDigit($aSplit[1]) Then Exit Msgbox(0,"", "failure")
;Valid code executes here (allows the direct further use of $aSplit)
Msgbox(0,"", "ok")

 

I see.  Very interesting.  I guess I need to brush up a bit more on the different things I can do with message boxes.  Thanks for your insight.  You too Chimp

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...