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 

addition numbers in one line

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
vlcek



Joined: 19 Feb 2007
Posts: 338
Location: Czech Republic

PostPosted: Mon Feb 22, 2010 4:16 pm    Post subject: addition numbers in one line Reply with quote

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
View user's profile Send private message Visit poster's website
polyethene



Joined: 11 Aug 2004
Posts: 5248
Location: UK

PostPosted: Mon Feb 22, 2010 4:18 pm    Post subject: Reply with quote

This should not be difficult with a parsing loop.
_________________
GitHubScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Mon Feb 22, 2010 4:19 pm    Post subject: Reply with quote

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
View user's profile Send private message
sinkfaze



Joined: 18 Mar 2008
Posts: 5043
Location: the tunnel(?=light)

PostPosted: Mon Feb 22, 2010 4:21 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
vlcek



Joined: 19 Feb 2007
Posts: 338
Location: Czech Republic

PostPosted: Mon Feb 22, 2010 5:45 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
garry



Joined: 19 Apr 2005
Posts: 2212
Location: switzerland

PostPosted: Mon Feb 22, 2010 6:02 pm    Post subject: Reply with quote

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
View user's profile Send private message
sinkfaze



Joined: 18 Mar 2008
Posts: 5043
Location: the tunnel(?=light)

PostPosted: Mon Feb 22, 2010 6:13 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
garry



Joined: 19 Apr 2005
Posts: 2212
Location: switzerland

PostPosted: Mon Feb 22, 2010 6:22 pm    Post subject: Reply with quote

thank you sinkfaze, it works
I was thinking that it's not possible to write commands in one line
Back to top
View user's profile Send private message
TLM



Joined: 21 Aug 2006
Posts: 2926
Location: The Shell

PostPosted: Mon Feb 22, 2010 6:33 pm    Post subject: Reply with quote

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
View user's profile Send private message
answer4u
Guest





PostPosted: Mon Feb 22, 2010 6:45 pm    Post subject: Reply with quote

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





PostPosted: Mon Feb 22, 2010 6:49 pm    Post subject: Reply with quote

+= 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

PostPosted: Mon Feb 22, 2010 7:40 pm    Post subject: Reply with quote

thank you all for the solutions / answers
Back to top
View user's profile Send private message
Display posts from previous:   
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