Jump to content
Sign in to follow this  
SeF

Reading content from specified line of a Variable

Recommended Posts

SeF

Hello there!

I'm creating a variable with a lot of content and don't know how can I extract the content from specified lines from this variable.

Creating the variable:

#include <IE.au3>
$oIE = _IEAttach ("Receita Federal do Brasil")
$oFrame = _IEFrameGetCollection ($oIE, 1)
$sHTML = _IEBodyReadText ($oFrame)

I've tryed using 2 different ways:

FileReadToArray

#include <file.au3>
Dim $lines
$file = FileOpen("teste.txt", 1)
FileWrite($file, $sHTML)
_FileReadToArray("teste.txt",$lines)
MsgBox(0, "Line read:", $lines[58])
FileDelete("teste.txt")

FileReadLine

$file = FileOpen("teste.txt", 1)
FileWrite($file, $sHTML)
$file = FileOpen("teste.txt", 0)
$line = FileReadLine($file, 58)
MsgBox(0, "Line read:", $line)
FileDelete("teste.txt")

A blank file is created and while the FileWrite is doing your "job", the FileReadLine tries to read the 58 line of the file. Result: returns a blank line. The "reading line part" it's going too fast (And I'm trying to forget a little bit of 'sleep' function in this case).

Can I read content from specified lines from a Variable? Or I really need to create a file to do this?

Thanks! :)


Share this post


Link to post
Share on other sites
Melba23

SeF,

If there is a simple delimiter as a line break (which seems likely), then you can use StringSplit directly on the variable to get the data into an array.

A thought (which you have probably investigated already) - are you sure that it is line 58 you need? Sometimes the lines are not quite what you think you counted - have you looked over a range of lines in case you are reading a blank line close by?

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
SeF

SeF,

If there is a simple delimiter as a line break (which seems likely), then you can use StringSplit directly on the variable to get the data into an array.

A thought (which you have probably investigated already) - are you sure that it is line 58 you need? Sometimes the lines are not quite what you think you counted - have you looked over a range of lines in case you are reading a blank line close by?

M23

Thanks for the reply M23! B)

I usually use StringSplit a lot and tryed using it here, but didn't worked out. B)

And yes, I'm sure that the line 58 has contents. I removed the FileDelete from my Script and it shows content.

-----EDIT-----

I did it! :)

#include <IE.au3>
#include <file.au3>
$oIE = _IEAttach ("Receita Federal do Brasil")
$oFrame = _IEFrameGetCollection ($oIE, 1)
$sHTML = _IEBodyReadText($oFrame)
_FileCreate(@TempDir & "test.txt")
_FileWriteToLine(@TempDir & "test.txt", 1, "old")
_ReplaceStringInFile(@TempDir & "test.txt", "old",  $sHTML)
sleep(10)
$file = FileOpen(@TempDir & "test.txt", 0)
$line = FileReadLine($file, 58)
MsgBox(0, "Line read:", $line)

File UDF is clearly faster! It's like...instantly! ;)

Edited by SeF

Share this post


Link to post
Share on other sites
Melba23

SeF,

I was afraid it would not be that easy! :)

If the file is not too large, could you upload it (or provide the code which downloads it) and say which line you expect as a return. Then we can take a closer look.

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
SeF

SeF,

I was afraid it would not be that easy! ;)

If the file is not too large, could you upload it (or provide the code which downloads it) and say which line you expect as a return. Then we can take a closer look.

M23

Thanks again M23! :)

But, I've already solved my problem.

The script is working like a charm!

Edited by SeF

Share this post


Link to post
Share on other sites
Melba23

SeF,

Would you care to say how? :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
SeF

SeF,

Would you care to say how? B)

M23

I edited my 2nd post of this topic with the solution B)

Anyway:

#include <IE.au3>
#include <file.au3>
$oIE = _IEAttach ("Receita Federal do Brasil")
$oFrame = _IEFrameGetCollection ($oIE, 1)
$sHTML = _IEBodyReadText($oFrame)
_FileCreate(@TempDir & "test.txt")
_FileWriteToLine(@TempDir & "test.txt", 1, "old")
_ReplaceStringInFile(@TempDir & "test.txt", "old",  $sHTML)
$file = FileOpen(@TempDir & "test.txt", 0)
$line = FileReadLine($file, 58)
MsgBox(0, "Line read:", $line)

Still not getting the information directly from the Variable. But, I get the results quickly after all. ;)

The file itself contains about 97-109 lines. It has different number of lines depending of the case, but always with a minimum of 97 lines. A simple code like this solved this issue too: B)

$NumLines = _FileCountLines("test.txt")
$i = $Numlines - 97

Then:

$line = FileReadLine($file, 58 + $i)

Still getting the hang of AutoIt! :)


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  

×