| View previous topic :: View next topic |
| Author |
Message |
[二oO也 Guest
|
Posted: Mon Feb 21, 2005 11:07 pm Post subject: String convert |
|
|
Simple question:
Is there any way to convert a string to a variable? |
|
| Back to top |
|
 |
Watcher
Joined: 28 Dec 2004 Posts: 60
|
Posted: Mon Feb 21, 2005 11:09 pm Post subject: Re: String convert |
|
|
| [二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 |
|
 |
[二oO也 Guest
|
Posted: Mon Feb 21, 2005 11:22 pm Post subject: Re: Re: String convert |
|
|
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
|
Posted: Mon Feb 21, 2005 11:39 pm Post subject: |
|
|
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 |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3004 Location: Minnesota
|
Posted: Mon Feb 21, 2005 11:41 pm Post subject: |
|
|
| Code: | loop,read,numbers.txt
{
if a_loopreadline is not number
continue
sum += %a_loopreadline%
}
msgbox,%sum%
return |
|
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Tue Feb 22, 2005 1:40 am Post subject: Re: String convert |
|
|
| 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 |
|
 |
Guest
|
Posted: Tue Feb 22, 2005 7:31 am Post subject: Re: String convert |
|
|
| 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.
|
Posted: Tue Feb 22, 2005 8:00 am Post subject: adding numbers |
|
|
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 |
|
 |
Dippy46
Joined: 06 Jul 2004 Posts: 171 Location: Manchester, England.
|
Posted: Tue Feb 22, 2005 8:02 am Post subject: |
|
|
or possibly 48 if your awake  _________________ Simple ideas lie within reach, only of complex minds |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3004 Location: Minnesota
|
Posted: Tue Feb 22, 2005 5:31 pm Post subject: |
|
|
Eh, show me up, will you? Well then, take this!
| Code: | loop,read,numbers.txt
sum += %a_loopreadline%
msgbox,%sum%
return |
Re-optimization! The brackets aren't necessary for one line!
That return isn't entirely necessary either, since AHK has "fall-off" support, so this is a three-liner. |
|
| Back to top |
|
 |
Dippy46
Joined: 06 Jul 2004 Posts: 171 Location: Manchester, England.
|
Posted: Tue Feb 22, 2005 6:23 pm Post subject: |
|
|
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.  _________________ Simple ideas lie within reach, only of complex minds |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3004 Location: Minnesota
|
Posted: Tue Feb 22, 2005 6:35 pm Post subject: |
|
|
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 |
|
 |
|