Jump to content

_ArraySearch Help


Recommended Posts

Hello, 

       Here is what I am trying to do.  I read a file to an array.  I then want to search the array for a string.  once that string is  found add to line to count and insert text.   Here is what I have so far from searching the forum. 

_FileReadToArray('C:\Temp\temp1.txt', $avArray, $FRTA_NOCOUNT)

_ArrayDisplay($avArray)

$search = "Transcriptionist:"

Local $iIndex =_ArraySearch($avArray, $search, 0, 0, 0, 1)+2

For $i = 0 to UBound($avArray, 2)-1
   Local $iIndex =_ArraySearch($avArray, $search, 0, 0, 0, 1)+2
       _FileWriteToLine($avArray, $iIndex, "          Encounter Info:")

Next
MsgBox(0, "Found", $search & " was found in the array at position [" & $iIndex & "]")

So this script only finds the first instance of $search.  Does not write text to the array.  Any help pointing me in the right direction would be great! Thank you

Link to comment
Share on other sites

Still a bit unclear of what you are exactly trying to do but it seems you are using wrong functions.

_ArraySearch only looks for the first time it occurs, you need to use _ArrayFindAll to return all instances of your search

_FileWriteToLine is used to write to files, not arrays.

I have made a working version of what I think you were trying to do:

_FileReadToArray($location, $avArray, $FRTA_NOCOUNT)

_ArrayDisplay($avArray)

$search = "Transcriptionist:"

;get location of ALL Transcriptionist
Local $iIndex =_ArrayFindAll($avArray, $search, 0, 0, 0, 1)

_ArrayDisplay($iIndex) ; Show locations of search

;Loop through array and add 2 and write to that line.
for $i = 0 to UBound($iIndex)-1 step 1
    $avArray[$iIndex[$i]+2] = $avArray[$iIndex[$i]+2] & "          Encounter Info:"
Next

_ArrayDisplay($avArray)

Hopefully you can mold this into what you were trying to accomplish.

Edited by Davidowicza
Code
Link to comment
Share on other sites

@xcaliber13
This will help you to store the count of lines where you have that search word:

Global $arrArray[5] = ["Transcriptionist:", "Other Text", "Transcriptionist", "Other Text", "Transcriptionist:"], _
       $strSearchText = "Transcriptionist:", _
       $intFoundCount = 0

For $i = 0 To UBound($arrArray) - 1 Step 1
    If StringInStr($arrArray[$i], $strSearchText) Then $intFoundCount += 1
Next

ConsoleWrite("The text '" & $strSearchText & "' has been found " & $intFoundCount & " times." & @CRLF)

 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Davidowicza,

            Exactly what I needed.   Works Great.  Just one question.  on this line:

$avArray[$iIndex[$i]+2] = $avArray[$iIndex[$i]+2] & "          Encounter Info:"

If I change the 2 to a 1 works.   If I change the 2 to a 3  I get an error.   Is that because on that third line there is already text there?

Link to comment
Share on other sites

Here is a few other examples, replacing the same item, appending text to the line item and inserting additional data two lines below the line item data,.  The problem is if with Davids code is that if the line item is at the bottom of the file you need to redim the array to add the additional two lines.

 

#include <Array.au3>
#include <File.au3>

Local $avArray
_FileReadToArray('C:\Temp\temp1.txt', $avArray, $FRTA_NOCOUNT)
    If @error Then Exit
_ArrayDisplay($avArray)

Local $sSearch = "Transcriptionist:"
Local $aSearch =_ArrayFindAll($avArray, $sSearch, 0, 0, 0, 1)
For $i = UBound($aSearch) - 1 To 0 Step -1
    ;~ Replace each line item with Transcriptionist: with data
    $avArray[$aSearch[$i]] = "Replaced Transcriptionist line item: "

    ;~ Append data to each line item with Transcriptionist:
    $avArray[$aSearch[$i]] &= "Appended Encounter Info: on the same Transcriptionist line item"

    ;~ Important when changing the array boundaries always loop backwards through the Array
    ;~ Add Encouter Info: above the line item with Transcriptionist
    If $aSearch[$i] + 2 > UBound($avArray) - 1 Then
        ReDim $avArray[UBound($avArray) + 2]
        $avArray[$aSearch[$i] + 2] = "Inserting Encounter Info: two lines below, Transcription: line item."
    Else
        _ArrayInsert($avArray, $aSearch[$i] + 2, "Inserting Encounter Info: two lines below, Transcription: line item.")
        ;~ nb: If you want to add above Transcriptionist line, just use _ArrayInsert($avArray, $aSearch[$i], "Encounter Info:")
    EndIf
Next
_ArrayDisplay($avArray)

MsgBox(0, "Found", $sSearch & " was found in the array the following number of times [" & UBound($aSearch) - 1 & "]")

 

Edited by Subz
Pasted to quickly
Link to comment
Share on other sites

@Subz Oh gosh you're right, completely forgot to check for that. 

Since you are reading from a file, you might not want to add to any lines outside of the original size of the array, so you can also changed the for loop of my original to this:

for $i = 0 to UBound($iIndex)-1 step 1 ;Loop through array
    ;Make sure you are not trying to write on a line outside of the array
    if Not ((UBound($avArray)-1) - ($iIndex[$i]+2)) < 2 Then
        $avArray[$iIndex[$i]+2] = $avArray[$iIndex[$i]+2] & "          Encounter Info:" ;add 2 and write to that line.
    EndIf
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...