| View previous topic :: View next topic |
| Author |
Message |
vlcek
Joined: 19 Feb 2007 Posts: 338 Location: Czech Republic
|
Posted: Mon Feb 22, 2010 4:16 pm Post subject: addition numbers in one line |
|
|
Good evening.
I returned to autohotkey.
Can you help me with this function?
In line 2 in file numbers.txt, I have this numbers:
2;1;3;4;6;3;7;3
I want to addition the numbers and divide number of numbers in line 2.
In line 3 I have other texts, in line 1 is note for using the file.
For reading a line, I use
filereadline, filecontent, numbers.txt, 2 _________________ Thanks. |
|
| Back to top |
|
 |
polyethene
Joined: 11 Aug 2004 Posts: 5248 Location: UK
|
Posted: Mon Feb 22, 2010 4:18 pm Post subject: |
|
|
This should not be difficult with a parsing loop. _________________ GitHub • Scripts • IronAHK • Contact by email not private message. |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Mon Feb 22, 2010 4:19 pm Post subject: |
|
|
Could you post a before after example? Also have a look at my TF library (links in signature below) as you can read and replace specific lines in text files as well or use a stringreplace or regexreplace etc _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
Posted: Mon Feb 22, 2010 4:21 pm Post subject: |
|
|
To expand on Titan's advice:
| Code: | FileReadLine, filecontent, numbers.txt, 2
Loop, Parse, filecontent, `;
result+=A_LoopField, count:=A_Index
average:= result / count
MsgBox %average%
return |
_________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
vlcek
Joined: 19 Feb 2007 Posts: 338 Location: Czech Republic
|
Posted: Mon Feb 22, 2010 5:45 pm Post subject: |
|
|
Why is a_index=0?
When I edit your code to:
result+=A_LoopField
count:=A_Index
average:= result / count
msgbox %result% %count% %average%
script shows the first result count is 0 and average is blank.
When I use your original code, the result is blank. _________________ Thanks. |
|
| Back to top |
|
 |
garry
Joined: 19 Apr 2005 Posts: 2212 Location: switzerland
|
Posted: Mon Feb 22, 2010 6:02 pm Post subject: |
|
|
I tried this with sinkfaze's script
| Code: | F1=%A_scriptdir%\numbers.txt
ifnotexist,%f1%
{
Fileappend,AAA`r`n,%f1%
Fileappend,2;1;3;4;6;3;7;3`r`n,%f1%
Fileappend,CCC`r`n,%f1%
}
A:=0
FileReadLine, filecontent, %f1%, 2
Loop, Parse, filecontent, `;
{
A:=(A+A_LoopField)
count:=A_Index
}
msgbox,result=%a%
average:= A / count
MsgBox,Average= %average%
return
|
|
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
Posted: Mon Feb 22, 2010 6:13 pm Post subject: |
|
|
Apparently the brackets must be there and the variable saves must be on separate lines. Strange...
| Code: | FileReadLine, filecontent, numbers.txt, 2
Loop, Parse, filecontent, `;
{
result+=A_LoopField
count:=A_Index
}
; MsgBox % result "`n" count
average:= result / count
MsgBox %average%
return |
_________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
garry
Joined: 19 Apr 2005 Posts: 2212 Location: switzerland
|
Posted: Mon Feb 22, 2010 6:22 pm Post subject: |
|
|
thank you sinkfaze, it works
I was thinking that it's not possible to write commands in one line |
|
| Back to top |
|
 |
TLM
Joined: 21 Aug 2006 Posts: 2926 Location: The Shell
|
Posted: Mon Feb 22, 2010 6:33 pm Post subject: |
|
|
| sinkfaze wrote: | | Apparently the brackets must be there and the variable saves must be on separate lines. Strange... | Interesting indeed.
At best its a normal outcome of the 2 expressions, at worse its a bug or an incompatibly issue (which could be bad for the age of scripts or ones running on different ahk versions).
I ran into the same thing with a recent function here. _________________
paradigm.shift:=(•_•)┌П┐RTFM||^.*∞ |
|
| Back to top |
|
 |
answer4u Guest
|
Posted: Mon Feb 22, 2010 6:45 pm Post subject: |
|
|
| Code: | result := 0 ;)
filecontent := "2;1;3;4;6;3;7;3"
Loop, Parse, filecontent, `;
result+=A_LoopField, count:=A_Index
average:= result / count
MsgBox %average%
return |
|
|
| Back to top |
|
 |
answer4u Guest
|
Posted: Mon Feb 22, 2010 6:49 pm Post subject: |
|
|
| += Documentation wrote: | | 3) The operators +=, -=, and *= treat blank variables as zero, but only when they are alone on a line; for example, y:=1, x+=1 and MsgBox % x-=3 both produce a blank result when x is blank. |
|
|
| Back to top |
|
 |
garry
Joined: 19 Apr 2005 Posts: 2212 Location: switzerland
|
Posted: Mon Feb 22, 2010 7:40 pm Post subject: |
|
|
| thank you all for the solutions / answers |
|
| Back to top |
|
 |
|