Jump to content

Searching array for multiple strings[solved]


barryb
 Share

Recommended Posts

Hi,

Is it possible to use _ArraySearch or some other feature to search for multiple keywords in my arrays? Or do I need to perform say a loop with _ArraySearch for each variable I'm searching for?

e.g. the array has 10 entries with different words but I only want say word 2, 5 and 9. This is just an example and they array key and words may change, the words I'm searching for however are constant.

Any pointers would be appreciated to get me going in the right direction :)

Thanks,

Barry

Edited by barryb
Link to comment
Share on other sites

Hi.

$MainArr=StringSplit("Joe,John,Mary,Jane,Jeff,Uwe,Sue",",")
$SearchArr=StringSplit("Joe,Michel,Sue",",")

for $i = 1 to $SearchArr[0]
    $found=False
    for $n = 1 to $MainArr[0]
        if $MainArr[$n]=$SearchArr[$i] Then
            $found=True
            ExitLoop
        EndIf
    Next
    if $found then
        MsgBox(0,"Found",$SearchArr[$i])
    Else
        MsgBox(0,"*NOT* found",$SearchArr[$i])
    EndIf
Next

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Hi,

with Rudis help my search works almost perfectly except when the code finds only one instance of the item being searched for.

The array created (in the code below) by $aArray = _StringBetween($sText, $start, $end) has string in element [0] e.g. [0] [DEF].

Apart from that it lists all the strings found between the the $start and $end variables. E.g. the text file can have 1 or more lines with ABCDEFGHI in it along with other text.

If the text file has only one instance I receive an error as the code provided by Rudi assumes element [0] will contain a count value, which it doesn't in this case, as it has the string found!

Here is my attempt at inserting an element [0] but it only works when there is more than one of the strings in the document and even then it is missing a found string.

Any ideas?

#include <array.au3>
#include <String.au3>
Dim $start, $end, $aArray, $iStart

$start = 'ABC'; where to start search from
$end = 'GHI'; where to end the search at
$sTextpath = "path to text filee.txt"
$sText = FileRead($sTextpath,FileGetSize($sTextpath))

Local $aArray[5] = [0]
Local $i
$aArray = _StringBetween($sText, $start, $end)
$count = UBound($aArray)
For $i = 1 To $count
   ; Check that the array is big enough
    If UBound($aArray) = $i Then 
       ; Resize the array
        ReDim $aArray[$aArray[0] + 5]
    EndIf
;Update Array
    $aArray[0] = $i
Next
;Adjust the array size. This time it is probably downward to the size of 
;$aArray[0] + 1 (remember the first item is $aArray[0]
ReDim $aArray[$aArray[0]+1]
;Now dump the result
_ArrayDisplay($aArray, 'Returned results')
Link to comment
Share on other sites

Hi,

Think I solved my issue, turned array back into a string "," delimited then split back into an array again :)

$aArray = _StringBetween($sText, $start, $end)
$sArray = _ArrayToString($aArray, ",")
$aArrayFound = StringSplit ($sArray,",")
Link to comment
Share on other sites

Hi,

Think I solved my issue, turned array back into a string "," delimited then split back into an array again :)

$aArray = _StringBetween($sText, $start, $end)
$sArray = _ArrayToString($aArray, ",")
$aArrayFound = StringSplit ($sArray,",")
You can make use of the function UBound() to get the elements of an array, see help file.

for $i=0 to UBound($DataArr) ; Thats basically just 0 to n-1, in stead of 1 to n, where n is the number of *data elements*
..

Stringsplit() will fill $Array[1]..$Array[n] with the results of the split, and sets

$Array[0]=UBound($Array)-1

Regards, Rudi.

[Edit: Typo]

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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