Jump to content

array problems


Recommended Posts

Hi all,

I have the following

#include <File.au3>
#include <Array.au3>
Dim $arr2,$finar
$edfd=@ScriptDir &"\result\"
If FileExists($edfd) = 0 Then DirCreate($edfd)
$arr = _FileListToArray(@ScriptDir, '*.txt', 1)
For $a = 1 To UBound($arr) - 1
    _FileReadToArray(@ScriptDir &"\"& $arr[$a], $arr2)
    for $a2 = (_ArraySearch($arr2, "    BB") + 1) to (_ArraySearch($arr2, " CC") - 1)
        if StringLen(StringStripWS($arr2[$a2],3))<>0 Then
            _ArrayAdd($finar,StringStripWS($arr2[$a2],3))
            ConsoleWrite(StringStripWS($arr2[$a2],3)&@CRLF)
        endif
    Next
    _ArrayDisplay($finar,"")
    _FileWriteFromArray($edfd & $arr[$a], $finar)
Next

i just cannot get the array $finar to hold the data 123 and 456 from the 2 files in scriptdir, may be a pair of fresh eyes could help?

Thanks:sweating:

1.txt

2.txt

Link to comment
Share on other sites

:)

Global $dirFileText = @ScriptDir
Global $dirResult = @ScriptDir & "\result"
If Not FileExists($dirResult) Then DirCreate($dirResult)
Global $fileResult = $dirResult & "\result.txt"
Global $aResult[0], $aFileList = _FileListToArray($dirFileText, '*.txt', 1, 1)

For $i = 1 To $aFileList[0]
    ConsoleWrite("-Working ON: " & $aFileList[$i] & @CRLF)
    Local $cFile = FileRead($aFileList[$i])
    Local $tmpResult = _StringBetween($cFile, "BB", "CC")
    If (Not @error) And IsArray($tmpResult) Then
        _ArrayAdd($aResult, $tmpResult)
        For $r = 0 To UBound($tmpResult) - 1
            Local $rmsp = StringStripWS($tmpResult[0], 8)
            ConsoleWrite("+Result: " & $rmsp & @CRLF)
            FileWriteLine($fileResult, $rmsp)
        Next
    EndIf
Next

_ArrayDisplay($aResult)

 

Edited by Trong

Regards,
 

Link to comment
Share on other sites

Thanks Trong  for the reply, however, what i try to achieve is

1. extract the data between the 2 lines( having BB and CC)in each file

2. skip the blank line that resulted from 1.

3. put the data from 2. and trim the white space of each file then put it back to a text file in another folder. i.e. 123 in 1.txt and 456 in 2.txt , both of them ended up in folder "Result"

And also, i would like to know what is the problem that my code have, so i can learn from it as i might need to do work with those array functions quite often.

Thanks

 

 

Link to comment
Share on other sites

@jjohn

Your script nearly worked. I added some comments
It's usually a better way to separate the instructions and place some error checking, this makes the script much easier to debug  :)

#include <File.au3>
#include <Array.au3>
Dim $arr2
$edfd=@ScriptDir &"\result\"
If FileExists($edfd) = 0 Then DirCreate($edfd)
$arr = _FileListToArray(@ScriptDir, '*.txt', 1)
For $a = 1 To UBound($arr) - 1
    Local $finar[0]   ; declare $finar as an array to get _ArrayAdd to work (and reinitialize it)
    _FileReadToArray(@ScriptDir &"\"& $arr[$a], $arr2)
    $start = _ArraySearch($arr2, "BB", 0, 0, 0, 1)+1   ; maybe it's more secure to do a partial search
    If @error Then ContinueLoop     ; better use this. if  _ArraySearch fails then the whole script crashes
    $end = _ArraySearch($arr2, "CC", 0, 0, 0, 1)-1   ; same
    If @error Then ContinueLoop
    for $a2 = $start to $end
        $txt = StringStripWS($arr2[$a2],3)
        if StringLen($txt)<>0 Then
            _ArrayAdd($finar, $txt)
            ConsoleWrite($txt&@CRLF)
        endif
    Next
    _ArrayDisplay($finar,"")
    _FileWriteFromArray($edfd & $arr[$a], $finar)
Next

 

Edited by mikell
Link to comment
Share on other sites

thanks mikell ,

yeah, you got the result i wanted, i will really compare mine code with yours to see what the difference.

Thanks for your help, and thanks to those people looked into this.

Link to comment
Share on other sites

For the fun, another way   :)

#include <File.au3>
#include <Array.au3>
$edfd=@ScriptDir &"\result\"
If not FileExists($edfd) Then DirCreate($edfd)
$arr = _FileListToArray(@ScriptDir, '*.txt', 1)
For $a = 1 To UBound($arr) - 1
    Local $finar[0]
    $res = StringRegExpReplace(FileRead(@ScriptDir &"\"& $arr[$a]), '(?s).*BB(.*?)CC.*', "$1")
    If not @extended Then ContinueLoop
    $res = StringStripWS($res, 3)
    if $res <> "" Then
        _ArrayAdd($finar, $res)
        ConsoleWrite($res&@CRLF)
    endif
    _ArrayDisplay($finar,"")
    _FileWriteFromArray($edfd & $arr[$a], $finar)
Next

 

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...