AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

String convert

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
[二oO也
Guest





PostPosted: Mon Feb 21, 2005 11:07 pm    Post subject: String convert Reply with quote

Simple question:
Is there any way to convert a string to a variable?
Back to top
Watcher



Joined: 28 Dec 2004
Posts: 60

PostPosted: Mon Feb 21, 2005 11:09 pm    Post subject: Re: String convert Reply with quote

[二oO也 wrote:
Simple question:
Is there any way to convert a string to a variable?


Umm, not sure that's a simple question. You have a variable that say contains the string Mystring and you want to create a new dynamic variable called Mystring? If that's the case I would guess the answer is no.
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
[二oO也
Guest





PostPosted: Mon Feb 21, 2005 11:22 pm    Post subject: Re: Re: String convert Reply with quote

What i'm trying to do is to open a file, say My file.txt that could look like this:
13
23
4
15
and so on...
Then pick a number at a time using Loop, Read, adding that number to a variable (Sum) and finaly showing the sum of all those numbers!
Back to top
Watcher



Joined: 28 Dec 2004
Posts: 60

PostPosted: Mon Feb 21, 2005 11:39 pm    Post subject: Reply with quote

Ok, based on your question you already know how to read the file, in your loop just add the strings

Myvar += %A_LoopField%

Not sure if the var is right but you should already know that part.
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
jonny



Joined: 13 Nov 2004
Posts: 3004
Location: Minnesota

PostPosted: Mon Feb 21, 2005 11:41 pm    Post subject: Reply with quote

Code:
loop,read,numbers.txt
{
 if a_loopreadline is not number
 continue
 sum += %a_loopreadline%
}
msgbox,%sum%
return
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10467

PostPosted: Tue Feb 22, 2005 1:40 am    Post subject: Re: String convert Reply with quote

Watcher wrote:
you want to create a new dynamic variable called Mystring?
Although it's not related to the question at hand, you can create a variable dynamically. The method is the same as that for creating arrays. The simplest example is probably:

Dynamic = MyNewVar
%Dynamic% = Value
Back to top
View user's profile Send private message Send e-mail
Guest






PostPosted: Tue Feb 22, 2005 7:31 am    Post subject: Re: String convert Reply with quote

Chris wrote:
you can create a variable dynamically. The method is the same as that for creating arrays. The simplest example is probably:

Dynamic = MyNewVar
%Dynamic% = Value


Cool! Didn't know you could do that!
Back to top
Dippy46



Joined: 06 Jul 2004
Posts: 171
Location: Manchester, England.

PostPosted: Tue Feb 22, 2005 8:00 am    Post subject: adding numbers Reply with quote

Jonny got it right
but the test is not necessay. Ahk automatically assignes the var as a number. if its not numeric it simple returns a zero
ie.
12
3
23
some text
3
4
; a blank
3

returns answer 47 Et voila!

Code:

loop,read,numbers.txt
{
 sum += %a_loopreadline%
}
msgbox,%sum%
return

_________________
Simple ideas lie within reach, only of complex minds
Back to top
View user's profile Send private message
Dippy46



Joined: 06 Jul 2004
Posts: 171
Location: Manchester, England.

PostPosted: Tue Feb 22, 2005 8:02 am    Post subject: Reply with quote

or possibly 48 if your awake Laughing
_________________
Simple ideas lie within reach, only of complex minds
Back to top
View user's profile Send private message
jonny



Joined: 13 Nov 2004
Posts: 3004
Location: Minnesota

PostPosted: Tue Feb 22, 2005 5:31 pm    Post subject: Reply with quote

Eh, show me up, will you? Well then, take this! Laughing

Code:
loop,read,numbers.txt
sum += %a_loopreadline%
msgbox,%sum%
return


Re-optimization! The brackets aren't necessary for one line! Cool

That return isn't entirely necessary either, since AHK has "fall-off" support, so this is a three-liner.
Back to top
View user's profile Send private message
Dippy46



Joined: 06 Jul 2004
Posts: 171
Location: Manchester, England.

PostPosted: Tue Feb 22, 2005 6:23 pm    Post subject: Reply with quote

I can't believe I didn't see that return. Well spotted sir. Mind U I didn't write the script did I, only corrected it. Laughing
_________________
Simple ideas lie within reach, only of complex minds
Back to top
View user's profile Send private message
jonny



Joined: 13 Nov 2004
Posts: 3004
Location: Minnesota

PostPosted: Tue Feb 22, 2005 6:35 pm    Post subject: Reply with quote

I actually meant the return at the end. This is a valid AHK script:

Code:
loop,read,numbers.txt
sum += %a_loopreadline%
msgbox,%sum%
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group