Sign in to follow this  
Followers 0
Shenghi

Range of numbers in a function

7 posts in this topic

Ok first of all, sorry if there already is a thread about this, but it's kinda hard to search for. If there already is a thread concerning this, please post the link and close this thread.

Now the problem. I'd like to supply a range of numbers for a function, for example _Rand(1-3) would pick a random number between 1 and 3. The _Rand() function is just used as an example in this case. My problem is that when using the following (logic) code...

Func _Rand($n)
    msgbox(0,"",$n)
EndFunc

...and calling it for example like...

_Rand(1-3)

...the messagebox will display '-2.' The obvious question is: "How can I get $n to be treated as plain text in AutoIt?"

Thanks in advance.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

MsgBox(64, 'Info:', 'The Help File has a "Random Function": ' & Random(1, 3, 1))

Edit:

Or if it absolutely has to be that way then:

$blah = _Ran('1-3')
MsgBox(64, 'Info:', $blah)

Func _Ran($iRandom)
    Local $aSplit = StringSplit($iRandom, '-')
    Return Random(Number($aSplit[1]), Number($aSplit[2]), 1)
EndFunc
Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

MsgBox(64, 'Info:', 'The Help File has a "Random Function": ' & Random(1, 3, 1))
Like I said, the _Rand thingie was just an example, to throw it to the case where I really need it: I am writing AutoIt UDF's for mIRC scripting 'Token Identifiers.' Some of them accept a range of tokens (like for example token 1 to 3) and the easiest way of supplying such a range (as you don't _have_ to supply one) would be the suggested method (1-3, for example). Thus the question remains: "How would I treat a supplied parameter in a function as plain text?"

Edit:

So I supose the only way is to supply it as a string.. A nuisance but oh well, can live with it :D

Edited by Shenghi

Share this post


Link to post
Share on other sites

Like I said, the _Rand thingie was just an example, to throw it to the case where I really need it: I am writing AutoIt UDF's for mIRC scripting 'Token Identifiers.' Some of them accept a range of tokens (like for example token 1 to 3) and the easiest way of supplying such a range (as you don't _have_ to supply one) would be the suggested method (1-3, for example). Thus the question remains: "How would I treat a supplied parameter in a function as plain text?"

Edit:

So I supose the only way is to supply it as a string.. A nuisance but oh well, can live with it :D

I think I answered that with my edit?

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Yes you did, I was just secretly hoping that it would be possible without supplying it as a string :]

Thanks anyway

Share this post


Link to post
Share on other sites

Yes you did, I was just secretly hoping that it would be possible without supplying it as a string :]

Thanks anyway

Hmm, if your getting it from ControlGetText() or whatever method your getting it as and storing it as a variable, it's comming in as a string anyway. Otherwise, there is no need for the function I provided.

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Hmm, if your getting it from ControlGetText() or whatever method your getting it as and storing it as a variable, it's comming in as a string anyway. Otherwise, there is no need for the function I provided.

...and if you get a number as a string, that you need as a number, use Int() or Number().

(SmOke_N was about to say that, I just wanted to beat him to it! :D )


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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