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 

grabbing floats for math
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
toralf



Joined: 31 Jan 2005
Posts: 3842
Location: Bremen, Germany

PostPosted: Thu Mar 03, 2005 2:53 pm    Post subject: Reply with quote

Ok, there were still some lines of code missing. But I thought you would figure it out by yourself. At least you found the missing % and corrected the error with the array names.
Here is a code that works. Customize it as you like. I hope this gives you now the right ideas on how to continue by yourself. Happy Scripting.
Code:
var1 := "text   text "
var2 := "12.34.5678 90:12   3.456   7.890   1.234   5.678 "

Loop, 2
  {
    FoundOneNumber := False
    Line := var%A_index%
    Line = %Line%
    loop
      {
        StringReplace,Line,Line, %A_Space%%A_Space% ,%A_Space%, All
        if ErrorLevel <> 0
              break
      }
    StringSplit, ArrayofNumbers, Line, %A_Space%
    ListOfNumbers =
    Loop, %ArrayofNumbers0%
      {
        Number := ArrayOfNumbers%A_Index%
        if Number is float
           {
             ListOfNumbers = %ListOfNumbers%|%Number%
             FoundOneNumber := True
           }
      }
    If FoundOneNumber
      {
        StringTrimLeft,ListOfNumbers ,ListOfNumbers ,1
        StringSplit, ArrayOfNumbers, ListOfNumbers , |
        Sum = 0
        MsgText =
        Loop, %ArrayofNumbers0%
          {
            Number := ArrayOfNumbers%A_Index%
            Sum := Sum + Number
            MsgText = %MsgText% + %Number%
           }
        MsgBox %Sum% = 0 %MsgText%
      }
  }

_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Guest






PostPosted: Fri Mar 04, 2005 10:30 am    Post subject: Reply with quote

Thanks toralf.
Here is the first working script so far. I stripped it down and removed the first part, since this must be read from another file (or clipboard). I also removed some of the other lines near the end. Suggestions?
This gives a general idea of what it's supposed to do.

Code:

Loop, Read, MyFile.txt
  {
    FoundOneNumber := False
     StringSplit, ArrayOfNumbers, A_LoopReadLine, %A_Tab%%A_Space%
    ListOfNumbers =
    Loop, %ArrayofNumbers0%
      {
        Number := ArrayOfNumbers%A_Index%
        if Number is float
           {
             ListOfNumbers = %ListOfNumbers%|%Number%
             FoundOneNumber := True
           }
      }
    If FoundOneNumber
      {
        StringTrimLeft, ListOfNumbers , ListOfNumbers ,1
        StringSplit, ArrayOfNumbers, ListOfNumbers , |
        Loop, %ArrayofNumbers0%
          {
            Number := ArrayOfNumbers%A_Index%
Float1 = %ArrayOfNumbers1%
Float2 = %ArrayOfNumbers2%
Float3 = %ArrayOfNumbers3%
Float4 = %ArrayOfNumbers4%
Total1 := Float1 + Float2
Total2 := Float3 * Float4
Total3 := Float2 / Float3
           }
       ; MsgBox %Float1% %Float2% %Float3% %Float4%
        MsgBox Total1 = %Total1% , Total2 = %Total2% , Total3 = %Total3%
      }
  }


MyFile.txt
Code:

text   text
12.34.5678 90:12   3.456   7.890   1.234   5.678

Back to top
toralf



Joined: 31 Jan 2005
Posts: 3842
Location: Bremen, Germany

PostPosted: Fri Mar 04, 2005 11:43 am    Post subject: Reply with quote

There isn't any question.
Does it work, or not?
????????????????????
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
toralf



Joined: 31 Jan 2005
Posts: 3842
Location: Bremen, Germany

PostPosted: Fri Mar 04, 2005 12:19 pm    Post subject: Reply with quote

Now I see,

Think about this loop:
Code:
Loop, %ArrayofNumbers0%
          {
            Number := ArrayOfNumbers%A_Index%
Float1 = %ArrayOfNumbers1%
Float2 = %ArrayOfNumbers2%
Float3 = %ArrayOfNumbers3%
Float4 = %ArrayOfNumbers4%
Total1 := Float1 + Float2
Total2 := Float3 * Float4
Total3 := Float2 / Float3
           }


What does it do?
And what is it that you want to do?
See the problem?

BTW: Please indent the lines nicely. It makes the reading more easily and might help you to find bugs.
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
dulcet



Joined: 05 Mar 2005
Posts: 4

PostPosted: Sat Mar 05, 2005 10:31 am    Post subject: Reply with quote

I went ahead and registered.
Ok, I know this is still not correct, because I commented out the last loop, and removed the %'s around the Float vars, and the code still perfoms the same. I have the feeling that the placement of the Float and/or Total vars may be the probelm. I've tried placing those in various locations with no difference. I left the commented out lines instead of removing them in case they may be actually needed.

The goal is to get 1 line consisting of 1 result for each of the Total vars:
11.346000, 7.00652, 6.393841

The strange thing is this:
When using the "MsgBox Total1= ..." line, the script does exactly what I want it to do, which is to show one result for each in the MsgBox.

BUT, if I use the FileAppend line, the script keeps calculating and appending incrementally to the file until it quickly grows past 100k. I've found that depending on the numbers used, the script will eventually stop after a monstrous append. So I know I've got some kind of continuous loop in the code, but not sure how to fix it.

Code:

Loop, Read, MyFile.txt
  {
    FoundOneNumber := False
     StringSplit, ArrayOfNumbers, A_LoopReadLine, %A_Tab%%A_Space%
    ListOfNumbers =
    Loop, %ArrayofNumbers0%
      {
        Number := ArrayOfNumbers%A_Index%
        if Number is float
           {
             ListOfNumbers = %ListOfNumbers%|%Number%
             FoundOneNumber := True
           }
      }
    If FoundOneNumber
      {
        StringTrimLeft, ListOfNumbers , ListOfNumbers ,1
        StringSplit, ArrayOfNumbers, ListOfNumbers , |
       ; Loop, %ArrayofNumbers0%
         ; {
           ; Number := ArrayOfNumbers%A_Index%
      Float1 := ArrayOfNumbers1
      Float2 := ArrayOfNumbers2
      Float3 := ArrayOfNumbers3
      Float4 := ArrayOfNumbers4
      Total1 := Float1 + Float2
      Total2 := Float3 * Float4
      Total3 := Float2 / Float3
          ; }
        ; MsgBox %Float1% %Float2% %Float3% %Float4%
         MsgBox Total1 = %Total1% , Total2 = %Total2% , Total3 = %Total3%
        ; FileAppend, `n %Total1% %Total2% %Total3% `n, Myfile.txt
      }
  }
Back to top
View user's profile Send private message
BoBo
Guest





PostPosted: Sat Mar 05, 2005 11:28 am    Post subject: Reply with quote

Quote:
So I know I've got some kind of continuous loop in the code
Guess your right.
You read this file:
Loop, Read, MyFile.txt
And you write/append to that:
FileAppend, `n %Total1% %Total2% %Total3% `n, Myfile.txt
Back to top
toralf



Joined: 31 Jan 2005
Posts: 3842
Location: Bremen, Germany

PostPosted: Sat Mar 05, 2005 2:06 pm    Post subject: Reply with quote

Hi, Dulcet
That's nice, now I know whom I talk to.

If you have a look in the manual, you can see that you can use this as well
Code:
FileDelete, MyNewFile.txt
Loop, Read, MyFile.txt , MyNewFile.txt
  {
    FoundOneNumber := False
    StringSplit, ArrayOfNumbers, A_LoopReadLine, %A_Tab%%A_Space%
    ListOfNumbers =
    Loop, %ArrayofNumbers0%
      {
        Number := ArrayOfNumbers%A_Index%
        if Number is float
           {
             ListOfNumbers = %ListOfNumbers%|%Number%
             FoundOneNumber := True
           }
      }
    If FoundOneNumber
      {
        StringTrimLeft, ListOfNumbers , ListOfNumbers ,1
        StringSplit, ArrayOfNumbers, ListOfNumbers , |
        Total1 := ArrayOfNumbers1 + ArrayOfNumbers2
        Total2 := ArrayOfNumbers3 * ArrayOfNumbers4
        Total3 := ArrayOfNumbers2 / ArrayOfNumbers3
        FileAppend, %Total1% %Total2% %Total3% `n
      }
  }


Not tested though.
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
dulcet



Joined: 05 Mar 2005
Posts: 4

PostPosted: Mon Mar 14, 2005 11:28 pm    Post subject: Reply with quote

toralf wrote:
Not tested though.


Tested and works great. Now I'm trying to add some extra stuff.
I think I need a funtion that doesn't exist - IfFormat.
The outputs contains extra numbers, which I would like to remove so that it is in the original format of the vars used in the calculation. I tried using StringLen and SetFormat, but as you can see, it doesn't work with vars of the same length.
I tried to describe as best I could in the commented section Find the Format.

Code:

Loop, Read, MyFile.txt , MyNewFile.txt
  {
    FoundOneNumber := False
     StringSplit, ArrayOfNumbers, A_LoopReadLine, %A_Tab%%A_Space%
    ListOfNumbers =
    Loop, %ArrayofNumbers0%
      {
        Number := ArrayOfNumbers%A_Index%
        if Number is float
           {
             ListOfNumbers = %ListOfNumbers%|%Number%
             FoundOneNumber := True
           }
      }
    If FoundOneNumber
      {
        StringTrimLeft, ListOfNumbers , ListOfNumbers ,1
        StringSplit, ArrayOfNumbers, ListOfNumbers , |
   ;
   ; Find the format and change the output to be the same.
   ; Description of purpose.
   ; If original format of ArrayOfNumberX is 1.2345,
   ; remove or trim output to X.XXXX
   ; If format is 123.45, trim output to XXX.XX
   ; If format is 12.34, trim output to XX.XX
   ; Examples:
   ; from output 1.234567   to 1.2345
   ; from output 123.456789 to 123.45
   ; from output 12.345678  to 12.34
   ;
   ; Note: Every ArrayOfNumbersX var on a given line have the same format,
   ; but each Line may be in a different format from each other.
   ; There is no combination of formats on a given line.
   ; Examples of ArrayOfNumbersX vars on a given line:
   ; 1.2345 2.3456 3.4567 4.5678
   ; 123.45 678.90 123.45 678.90
   ; 12.34 56.78 90.12 34.56
   ;
      ; StringLen, floatlen, ArrayOfNumbers1
      ; if floatlen = 6
         ; SetFormat, float, 0.4 ; Won't work, 1.2345 123.45
                  ; have the same length.
      ; else if floatlen = 5
         ; SetFormat, float, 0.2 ; for 12.34 floats
   ;
         ; Total1 += 0
         ; Total2 += 0
         ; Total3 += 0
   ;
   ; End of Find the format and change output.
   ;
        Total1 := ArrayOfNumbers1 + ArrayOfNumbers2
        Total2 := ArrayOfNumbers3 * ArrayOfNumbers4
        Total3 := ArrayOfNumbers2 / ArrayOfNumbers3
        FileAppend, `n%Total1% %Total2% %Total3%`n
      }
  }
Back to top
View user's profile Send private message
toralf



Joined: 31 Jan 2005
Posts: 3842
Location: Bremen, Germany

PostPosted: Tue Mar 15, 2005 6:53 am    Post subject: Reply with quote

Dear Dulcet,

Add into the loop "StringLeft, OutputVar, InputVar, Count"
So that it reads:
Code:
       Number := ArrayOfNumbers%A_Index%
        if Number is float
           {
             StringLeft, Number , Number, 6
             ListOfNumbers = %ListOfNumbers%|%Number%
             FoundOneNumber := True
           }


This trims away the numbers. Be careful if the number gets bigger than 999999 then this trimming operation shouldn't be done. (insert a if statement)

If you want to round up or down, use "transform".

Edit: 2005-03-15 Changed 5 to 6, since you wanted to have 6 characters, not five.
_________________
Ciao
toralf


Last edited by toralf on Tue Mar 15, 2005 4:21 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jonny



Joined: 13 Nov 2004
Posts: 3004
Location: Minnesota

PostPosted: Tue Mar 15, 2005 3:05 pm    Post subject: Reply with quote

Did you mean StringTrimLeft?
Back to top
View user's profile Send private message
toralf



Joined: 31 Jan 2005
Posts: 3842
Location: Bremen, Germany

PostPosted: Tue Mar 15, 2005 4:19 pm    Post subject: Reply with quote

No,
I mean StringLeft. I assume the numbers have trailing zeros.

Do they have leading zeros?

Why do you ask? Have I overlooked something?
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page Previous  1, 2, 3
Page 3 of 3

 
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