AutoHotkey Community

It is currently May 25th, 2012, 8:10 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: String convert
PostPosted: February 21st, 2005, 11:07 pm 
Simple question:
Is there any way to convert a string to a variable?


Report this post
Top
  
Reply with quote  
 Post subject: Re: String convert
PostPosted: February 21st, 2005, 11:09 pm 
Offline

Joined: December 28th, 2004, 12:33 am
Posts: 60
[¤GoO¤] 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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: Re: String convert
PostPosted: February 21st, 2005, 11:22 pm 
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!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 21st, 2005, 11:39 pm 
Offline

Joined: December 28th, 2004, 12:33 am
Posts: 60
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 21st, 2005, 11:41 pm 
Offline

Joined: November 13th, 2004, 4:08 am
Posts: 2951
Location: Minnesota
Code:
loop,read,numbers.txt
{
 if a_loopreadline is not number
 continue
 sum += %a_loopreadline%
}
msgbox,%sum%
return


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: String convert
PostPosted: February 22nd, 2005, 1:40 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: String convert
PostPosted: February 22nd, 2005, 7:31 am 
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!


Report this post
Top
  
Reply with quote  
 Post subject: adding numbers
PostPosted: February 22nd, 2005, 8:00 am 
Offline

Joined: July 6th, 2004, 10:07 am
Posts: 171
Location: Manchester, England.
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2005, 8:02 am 
Offline

Joined: July 6th, 2004, 10:07 am
Posts: 171
Location: Manchester, England.
or possibly 48 if your awake :lol:

_________________
Simple ideas lie within reach, only of complex minds


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2005, 5:31 pm 
Offline

Joined: November 13th, 2004, 4:08 am
Posts: 2951
Location: Minnesota
Eh, show me up, will you? Well then, take this! :lol:

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


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

That return isn't entirely necessary either, since AHK has "fall-off" support, so this is a three-liner.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2005, 6:23 pm 
Offline

Joined: July 6th, 2004, 10:07 am
Posts: 171
Location: Manchester, England.
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. :lol:

_________________
Simple ideas lie within reach, only of complex minds


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2005, 6:35 pm 
Offline

Joined: November 13th, 2004, 4:08 am
Posts: 2951
Location: Minnesota
I actually meant the return at the end. This is a valid AHK script:

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


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: kwfine, rbrtryn, vsub and 50 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group