Jump to content

_ArrayExtract extract lines from array who lines contain some string


jayfry
 Share

Recommended Posts

Hello, First this form is great, I've gotten so much done today with just the help files and examples here. So please if you know a help file I can use, let me know. _ArrayExtract is what i'm thinking will work.

What i'm doing: I'm getting a list of files whose name contains ".url", putting the full path to an Array, then i'm taking those files and joining all the files into 1 file. What I want to do now, is from that master file I created, I want to extract only lines whose it contains "URL". 

#Include <File.au3>
#Include <Array.au3>


$fileout = "C:\Users\Good Stuff\out.txt"
$sAutoItDir = "C:\Users\Store Bookmarks"
if FileExists($fileout) then FileDelete($fileout)
$file = FileOpen($fileout, 1)

$aArray = _FileListToArrayRec($sAutoItDir, "*.url", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)

for $i = 1 to $aArray[0]
    $data = FileRead($aArray[$i])
    FileWrite($file, "-------------------- File:  "&$aArray[$i]&"--------------------------------"&@crlf)
    FileWrite($file, $data&@CRLF)
Next

; Define a variable to pass to _FileReadToArray.
Local $aArray = 0


If Not _FileReadToArray($fileout, $aArray, 0) Then
    MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file.
EndIf

; Display the array in _ArrayDisplay.
_ArrayDisplay($aArray)

;Here I want to create a variable and set it to the value of an array whose consists of only lines that have URL, extracted from the master file i loaded into an array.

;$extractedArray = _ArrayExtract($aArray, "URL=", "URL=")
;_ArrayDisplay($extractedArray)

FileClose($file)
;ShellExecute($fileout)

 

Everything works the way I want it to, up until extracting lines whose it contains URL from the "master file" array I made.

 

Hoping someone can point me in the right direction.  Also I just started autoit today so if you see other issues please feel free to point them out to me. 

Link to comment
Share on other sites

use arrayfindall to get the indices, then loop through that array putting the values back.

#include<array.au3>

local $a[9]=[1,2,'line 3 has url in it',4,5,6,'url begins line 7','line 8 ends with url',9]

 $aMatches = _ArrayFindAll($a , "url" , 0 ,0 , 0, 1)

For $i = 0 to ubound($aMatches) - 1
     $aMatches[$i] = $a[$aMatches[$i]]
Next

_ArrayDisplay($aMatches)

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

I pull the entire file into a array then loop the array and check for the ITEM containing the word

 

$file1 = "c:\somefile.txt"
; ---------------------------------
; FILE TO ARRAY
; ---------------------------------
Func _GetValid()
; ..................................................
$ListRead = FileRead($file1, FileGetSize($file1))
$aarray = StringRegExp($ListRead, "(?i)url(.*?)", 3)  ;; THIS IS THE PART YOU NEED TO CHANGE TO FIND YOUR LINES 
; ..................................................
For $bi = 0 to UBound($aarray) - 1
ConsoleWrite("DEBUG: " & $aarray[$bi]  & @CRLF)   ;  THIS IS THE PART YOU NEED TO EXPORT TO A FILE LINE BY LINE
Next
; ..................................................
Return
EndFunc

;also try _StringExplode($sText, "##", 0)
;StringInStr("This is a sentence with whitespace.", "white")

 

Link to comment
Share on other sites

one more go at it, think this line might pull off the trick

#include<array.au3>

local $a[9]=['entry 0 url match' , 1,2,'line 3 has url in it',4,5,6,'url begins line 7','line 8 ends with url']

 _ArrayDisplay(StringRegExp(_ArrayToString($a, @LF) & @LF , "(.*url.*?)" & @LF , 3))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

So my output gave me more URLs than I wanted, and I stumbled across stringleft.

I'm now using the following to sort go through my array and only take the lines that start with URL. 

 

#Include <File.au3>
#Include <Array.au3>
$fileout = "C:\Users\Jonathan\Desktop\DEV\CreateFile\Good Stuff\out.txt"
$sAutoItDir = "C:\Users\Jonathan\Desktop\DEV\Store Bookmarks"
if FileExists($fileout) then FileDelete($fileout)
$file = FileOpen($fileout, 1)

;This buils out the Dirs to get bookmarks from. It recurselivy returns files that end in .url. 
$aArray = _FileListToArrayRec($sAutoItDir, "*.url", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)

;This takes the file paths provided by the _FilelisttoarrayRec above and combines all the files into 1 file.

for $i = 1 to $aArray[0]
    for $j = 1 to _FileCountLines($aArray[$i])
        $line = FileReadline($aArray[$i],$j)
            If StringLeft($line, 3) = "URL" Then
            FileWrite($file, $line&@CRLF)
        EndIf
    Next
Next

 

Let me know if this is good or not :D

Link to comment
Share on other sites

probably going to want to look at _FileReadToArray or at least FileOpen and pass the handle (as you did with the outfile).

Please note this from the FileReadLine help file:

Quote

If a filename is passed to the function, the file will be opened and closed during the call which slows execution considerably

based off its location in the loop you are opening-reading-closing for every line of every file.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

41 minutes ago, iamtheky said:

probably going to want to look at _FileReadToArray or at least FileOpen and pass the handle (as you did with the outfile).

Please note this from the FileReadLine help file:

based off its location in the loop you are opening-reading-closing for every line of every file.

Thank you for pointing that out.

I think this fixes that issue.

for $i = 1 to $aArray[0]
    for $j = 1 to _FileCountLines($aArray[$i])
        $line = FileReadline($aArray[$i],$j)
            If StringLeft($line, 3) = "URL" Then
            FileWrite($file, $line&@CRLF)
        EndIf
    Next
Next

Could you point me to which help file will show me how to add the result into another array, i'd rather not save this to a file if I don't have to. 

I Tired _ArrayAdd but i'm not getting it. I think it's the right direction tho. 

 

Link to comment
Share on other sites

44 minutes ago, iamtheky said:

probably going to want to look at _FileReadToArray or at least FileOpen and pass the handle (as you did with the outfile).

Please note this from the FileReadLine help file:

based off its location in the loop you are opening-reading-closing for every line of every file.

Thank you for pointing that out.

I think this fixes that issue.

for $i = 1 to $aArray[0]
    $file2 = $aArray[$i]
    FileOpen($file2,0)
    For $j = 1 to _FileCountLines($file2)
        $line = FileReadLine($file2, $j)
        If StringLeft($line, 3) = "URL" Then
            FileWrite($file, $line&@CRLF)
        EndIf
    Next
Next

Could you point me to which help file will show me how to add the result into another array, i'd rather not save this to a file if I don't have to. 

I Tired _ArrayAdd but i'm not getting it. I think it's the right direction tho. 

 

Edit. Adding wrong code at first.

Link to comment
Share on other sites

ArrayAdd is probably useful.

Im thinking _FileReadToArray, delete everything without URL, then build a Final array with the leftovers each time through.

the below is just pseudo code, may or may not work at all: 

 

local $aFinal[0]
local $aArray

For $j = 0 to ubound($aListOfFiles) - 1

 _FileReadToArray($ListofFiles[$j] , $aArray , 0)

  For $i = ubound($aArray) - 1 to 0 step -1 ;note to loop backwards if you are deleting stuff

      if stringleft($aArray[$i],3) <> "URL"  then _ArrayDelete($aArray , $i)

  Next

_ArrayAdd($aFinal , $aArray)

Next

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

I figured out I was initializing my array inside my loop (DOH).

This works for me.

 

local $aFinal[0]
for $i = 1 to $aArray[0]
    $file2 = $aArray[$i]
    FileOpen($file2,0)
    For $j = 1 to _FileCountLines($file2)
        $line = FileReadLine($file2, $j)
        If StringLeft($line, 3) = "URL" Then
            _ArrayAdd($aFinal, $line)
        EndIf
    Next
Next
_ArrayDisplay($aFinal)

 

Link to comment
Share on other sites

to round this out in case anyone wants to use it later Here's my full code.

 

#Include <File.au3>
#Include <Array.au3>
$sAutoItDir = "C:\Users\Jonathan\Desktop\DEV\Store Bookmarks"

;This buils out the Dirs to get bookmarks from. It recurselivy returns files that end in .url. 
$aArray = _FileListToArrayRec($sAutoItDir, "*.url", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)

;This takes the files provided by $aArray, Reads them line by line, and for lines that start with URL, it writes them to $aFinal
local $aFinal[0]
for $i = 1 to $aArray[0]
    $file2 = $aArray[$i]
    FileOpen($file2,0)
    For $j = 1 to _FileCountLines($file2)
        $line = FileReadLine($file2, $j)
        If StringLeft($line, 3) = "URL" Then
            _ArrayAdd($aFinal, $line)
        EndIf
    Next
Next
_ArrayDisplay($aFinal)

 

i'm super happy. The documentation for this language is great. 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...