Jump to content
Sign in to follow this  
jimmyjoe

Reading a line in file question

Recommended Posts

jimmyjoe

I know how to find open and read the line in a file. What I need to know is how in that line, do I have it look for a a certain word/phrase to get the numbers after the word/phrase. Like for example:

Line 52: blah blah is 20 and blah blah is 30 health is 40

I need for it to look for the phrase "health is" and then input the number after which i will set as a variable for other parts of the script.

So find the keyword/phrase and read the 2 or 3 digits after it.

Share this post


Link to post
Share on other sites
PsaltyDS

I know how to find open and read the line in a file. What I need to know is how in that line, do I have it look for a a certain word/phrase to get the numbers after the word/phrase. Like for example:

Line 52: blah blah is 20 and blah blah is 30 health is 40

I need for it to look for the phrase "health is" and then input the number after which i will set as a variable for other parts of the script.

So find the keyword/phrase and read the 2 or 3 digits after it.

StringRegExp() would be shortest and quickest, but requires some geekiness to code the pattern.

You could also find the location with StringInStr() and then extract the text with StringMid().

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
jimmyjoe

I cant seem to figure this out. Ok heres what I have"

$file = FileOpen("C:\Documents and Settings\Dean\Desktop\Canvas.aspx_files\render.htm", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
$line = FileReadLine($file, 52)

Which will open this line

</td><td style="padding-left: 4px; padding-right: 4px; color: white; font-size: 11px;">Health: 80/100</td><td

[Above is just snippet of line total size of line is around 20x that half in front and half behind snippet]

You see the Health: 80/100 in the line I want it to extract the 80 or what ever number that may be (will change constantly). Then I can save that result as a var for future use.

Could someone throw together something I could work with for that.

Thx in advance

Share this post


Link to post
Share on other sites
PsaltyDS

I cant seem to figure this out. Ok heres what I have"

$file = FileOpen("C:\Documents and Settings\Dean\Desktop\Canvas.aspx_files\render.htm", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
$line = FileReadLine($file, 52)

Which will open this line

</td><td style="padding-left: 4px; padding-right: 4px; color: white; font-size: 11px;">Health: 80/100</td><td

[Above is just snippet of line total size of line is around 20x that half in front and half behind snippet]

You see the Health: 80/100 in the line I want it to extract the 80 or what ever number that may be (will change constantly). Then I can save that result as a var for future use.

Could someone throw together something I could work with for that.

Thx in advance

Although there is a steep learning curve when you first try regular expressions, they are very quick and efficient for this kind of thing:
#include <Array.au3>

$sString = '</td><td style="padding-left: 4px; padding-right: 4px; color: white; font-size: 11px;">Health: 80/100</td><td>'

$avRET = StringRegExp($sString, "(?:Health: )(\d+/\d+)", 3)
If IsArray($avRET) Then
    _ArrayDisplay($avRET, "Result")
Else
    MsgBox(16, "Error", "RegExp pattern found no match.")
EndIf

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.