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 1, 2, 3  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Sat Feb 19, 2005 3:29 am    Post subject: grabbing floats for math Reply with quote

I'm a newbie at this.
I'm trying to grab some floating numbers to be processed with various calculations. The file format I'm attempting to grab from is below.

---
Text Text Textt Text Txt
12.34.5678 12:34 1.2345 2.3456 3.4567 4.5678

---

All of the "spaces" are tabs, except for the one preceding the numbers
with a colon (Smile. I need to get the 4 sets of floats, after the colon number, and asign them to a variable, and then perform the various math calculations on the variables. From there, I would like to be able to paste the results on the 2nd empty line below, in a high to low order separated by spaces.
This is probably very easy for many of you, but this is what I've tried, but get no results. Here I'm actually trying to get the last 3 floating numbers.

; -----
FileRead, testvar, file.txt
if var is float
StringTrimLeft, Num2, float, 24
StringTrimLeft, Num3, float, 40
StringTrimLeft, Num4, float, 48
; This is just to test if the variable is there.
MsgBox, %Num2%
MsgBox, %Num3%
MsgBox, %Num4%
; ---------

I've tried different trim numbers and still can't seem to get the variables.
Laughing is ok.
Back to top
Invalid User



Joined: 14 Feb 2005
Posts: 442
Location: Texas, Usa

PostPosted: Sat Feb 19, 2005 3:53 am    Post subject: Reply with quote

StringSplit, OutputArray, InputVar [, Delimiters, OmitChars, FutureUse]

This should work

"For example: `, (an escaped comma) would divide the string based on every occurrence of a comma. Similarly, %A_Tab%%A_Space% would create a new array element every time a space or tab is encountered in InputVar"

This is from the delimiter area, so you could find each floating # by the space between them

Help?
_________________
my lame sig Smile
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Guest






PostPosted: Sat Feb 19, 2005 4:04 am    Post subject: Reply with quote

Thanks Invalid, I'll try that.
I did just realize that I do need to grab the 4 sets separated by tabs and not by a fixed place, because other sets of floats will be different.
some will be like this:
1.2345 2.3456 3.4567 4.5678

and some will be like this:
12.34 56.78 90.12 34.56

all separated by tabs.
But how do get only the last 4 sets? Could I delimit the first 2 parts some how?

these2 parts needs to be ignored these 4 are needed
12.34.5678 12:34 1.2345 2.3456 3.4567 4.5678
Back to top
Guest






PostPosted: Sat Feb 19, 2005 4:07 am    Post subject: Reply with quote

that didn't print out right.

these first 2 parts needs to be ignored or delimited.
12.34.5678 12:34

these 4 are needed.
1.2345 2.3456 3.4567 4.5678

They are all on the same line as in the first post.
Back to top
Invalid User



Joined: 14 Feb 2005
Posts: 442
Location: Texas, Usa

PostPosted: Sat Feb 19, 2005 4:38 am    Post subject: Reply with quote

Code:

MyNumbers = 12.34.5678_12:34_1.2345_2.3456_3.4567_4.5678
StringSplit, MyNumberArray, MyNumbers, `_
MsgBox, my first section:%MyNumberArray1% `nmysecond: %MyNumberArray2% `nthird : %MyNumberArray3% `nAnd fourth: %MyNumberArray4%




I think this is what you want, Use %A_Space% in place of the _
I just did that so it was easy to read. if you only need the last two sets of results use array3 and 4

lemme know how it works, by the way, that code is untested

EDIT:
I just noticed that there was 6 elements in that array so I hope that code doenst confuse you
_________________
my lame sig Smile
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Guest






PostPosted: Sun Feb 20, 2005 7:21 am    Post subject: Reply with quote

Invalid User wrote:
Code:

MyNumbers = 12.34.5678_12:34_1.2345_2.3456_3.4567_4.5678
StringSplit, MyNumberArray, MyNumbers, `_
MsgBox, my first section:%MyNumberArray1% `nmysecond: %MyNumberArray2% `nthird : %MyNumberArray3% `nAnd fourth: %MyNumberArray4%




Thanks Invalid. One thing I left out, though, is that the numbers change. They are not static, so I would not be able to place them into the ahk before hand. I could, but that would defeat the purpose. I guess what I need is for ahk to read a line, find any and all floats starting at a certain position based on a combination of either characters, spaces, and tabs, and then assisgn those floats to a variable.
Back to top
Invalid User



Joined: 14 Feb 2005
Posts: 442
Location: Texas, Usa

PostPosted: Sun Feb 20, 2005 7:48 am    Post subject: Reply with quote

hmm Are these numbers in a file, or in a "status" like text box in another application, ...LIKE in an editor of somesort that is

if they are in a file see these in the help
FileRead
FileReadLine
Loop, Read
(and probly most of the related subject within each)

if they are text in a control in a an App maybe

ControlGet
ControlGetText
perhaps, WinGetText
_________________
my lame sig Smile
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
BoBo
Guest





PostPosted: Sun Feb 20, 2005 9:01 am    Post subject: Reply with quote

Quote:
They are not static, so I would not be able to place them into the ahk before hand
To proof the concept, and to provide a working sample Invalid User had to create a var (MyNumbers) within the script (and because you haven't mentioned where you get yours from). Of course you can take its values from whatever external source as well.
Code:
MyNumbers = 12.34.5678_12:34_1.2345_2.3456_3.4567_4.5678 ; sample input
StringSplit, MyNumberArray, MyNumbers, `_ ; separator has to be replaced with %A_Tab%

ArrayToIgnore = %MyNumberArray0%
ArrayToIgnore -= 4 ; get only the last four arrays

Loop, %MyNumberArray0% ; read all arrays
{
    StringTrimRight, CurrentArray, MyNumberArray%A_Index%, 0
    If A_Index <= %ArrayToIgnore%
        Continue
    MsgBox, will do some math with MyNumberArray%A_Index%`n%CurrentArray% ; here you can do your math task
}
Invalid User has already point you in the right direction !
Cool
Back to top
Invalid User



Joined: 14 Feb 2005
Posts: 442
Location: Texas, Usa

PostPosted: Sun Feb 20, 2005 9:08 am    Post subject: Reply with quote

Good call BoBo, I was gonna write some examples simular to yours there, but I am busy, I bet he/she(?) will get that with all the comments there. Nice
_________________
my lame sig Smile
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Guest






PostPosted: Mon Feb 21, 2005 5:28 am    Post subject: Reply with quote

Code:
MyNumbers = 12.34.5678_12:34_1.2345_2.3456_3.4567_4.5678 ; sample input
StringSplit, MyNumberArray, MyNumbers, `_ ; separator has to be replaced with %A_Tab%

ArrayToIgnore = %MyNumberArray0%
ArrayToIgnore -= 4 ; get only the last four arrays

Loop, %MyNumberArray0% ; read all arrays
{
    StringTrimRight, CurrentArray, MyNumberArray%A_Index%, 0
    If A_Index <= %ArrayToIgnore%
        Continue
    MsgBox, will do some math with MyNumberArray%A_Index%`n%CurrentArray% ; here you can do your math task
}


Thanks Bobo. You and Invalid have given great ideas and suggestions to start.
The data originally gets copied to the cliipboard from a program, and from there it is pasted into a text file. The program does not allow a way to retrieve or look at the data until it is copied to the clipboard.
Back to top
Guest






PostPosted: Tue Feb 22, 2005 2:43 am    Post subject: Reply with quote

Using a Loop example, I inserted an if float and it worked, as far as I can tell. Suggestions?

Code:

Loop, read, file.txt

Loop, parse, A_LoopReadLine, %A_Tab%%A_Space%
   {
if A_LoopField is float
      MsgBox, Field number %a_index% is %A_LoopField%
   }
}



This seemed to work with a file with these contents.
Code:

text   text
12.34.5678 90:12   3.456   7.890   1.234   5.678



This grabs the 4 floats as "Field Number" 3 - 6. Now for the next stupid question. Are the variable's names 3 thru 6? As in %3%, etc.?
Back to top
toralf



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

PostPosted: Tue Feb 22, 2005 8:29 am    Post subject: Reply with quote

You could use StringSplit to get the A_LoopReadLine into an Array. Then you could check the array items individually if they are float and use them for your math.
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Guest






PostPosted: Fri Feb 25, 2005 4:15 am    Post subject: Reply with quote

toralf wrote:
You could use StringSplit to get the A_LoopReadLine into an Array. Then you could check the array items individually if they are float and use them for your math.


I've tried this but can't seem to get the syntax right. It either does nothing or gets an error. Could you give an example?
Back to top
BoBo
Guest





PostPosted: Fri Feb 25, 2005 11:28 am    Post subject: Reply with quote

Quote:
Clipboard = 12.34.5678_12:34_1.2345_2.3456_3.4567_4.5678 ; sample input
FileAppend, %Clipboard%`n, MyFile.txt
; test section


Code:
Loop, Read, MyFile.txt
{
StringSplit, MyNumberArray, A_LoopReadLine, %A_Tab%

ArrayToIgnore = %MyNumberArray0%
ArrayToIgnore -= 4 ; get only the last four arrays/fields/numbers

Loop, %MyNumberArray0% ; read all arrays
{
    StringTrimRight, CurrentArray, MyNumberArray%A_Index%, 0
    If A_Index <= %ArrayToIgnore%
        Continue
    MsgBox, will do some math with MyNumberArray%A_Index% = %CurrentArray% ; here you can do your math task
}


So, where's the problem ?
Back to top
toralf



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

PostPosted: Fri Feb 25, 2005 1:32 pm    Post subject: Reply with quote

Just a small tweak, the statement
Quote:
StringTrimRight, CurrentArray, MyNumberArray%A_Index%, 0
can be replaced with
Code:
CurrentArray := MyNumberArray%A_Index%
That's easier to understand/read, since it's straight forward

And the loop you can shorten to
Code:
Loop, %ArrayToIgnore% ; read all arrays
{
    Number := MyNumberArray%A_Index%
    MsgBox, will do some math with MyNumberArray%A_Index% = %Number%
}


And since I have written already so much, I do the rest too:
Code:
Loop, Read, MyFile.txt
  {
    StringSplit, ArrayOfNumbers, A_LoopReadLine, %A_Tab%
    NumbersOfInterest := ArrayOfNumbers0 - 4
    Loop, %NumbersOfInterest% ; read all arrays
      {
        Number := ArrayOfNumbers%A_Index%
        MsgBox, I will do some math with ArrayOfNumbers%A_Index% = %Number%
      }
  }

_________________
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 1, 2, 3  Next
Page 1 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