richietheprogrammer Posted April 5, 2012 Share Posted April 5, 2012 Hello all, its been a while, good to be back . Trying to search a string from a substring in a csv file, and then display the whole string. For example, if I type "aut", I want a messagebox back that displays "autoit" if the word "autoit" is in the csv file. The following works, but only displays the first instant. How can I make it return all instances, and using something like arraydisplay, be able to choose an item from the list of results? Code I have that works only for the first instant: $csv=0 $input= "test" _FileReadToArray("File.csv",$csv) $searched= $input $searches= _arraysearch($csv,$searched,0,0,0,1,1,0) $record1=StringSplit($csv[$searches],",",0) Msgbox(0,"",$record1) The above example returs "testing" which is in the first cell in the csv file. theres also "testers" in the third row, but obviously the Msgbox is only returning "testing". So how can I use _arraysearch to grab and display all instants, and be able to select a specific instant from the list? Any help is greatly apreciated!! Link to comment Share on other sites More sharing options...
GEOSoft Posted April 5, 2012 Share Posted April 5, 2012 in the help file you will find a function called _ArrayFindAll use the partial search parameter. you could also use a regexp to get what you want by just using a fileread instead of _filereadtoarray George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 5, 2012 Moderators Share Posted April 5, 2012 richietheprogrammer, I would use a RegEx like this: expandcollapse popup#include <Array.au3> ; Declare an array to hold the instances Global $aListing[1][3] =[[0, 0, 0]] ; Simulate a CSV file $sCSV = "Autoit,Autarky,automatic,not_matched,Autumnal" ; Define the test string to search for $sTest = "aut" ; Start looking $iOffset = 1 While 1 ; Find next instance of the test string StringRegExp($sCSV, "(?i)" & $sTest, 1, $iOffset) If @error Then ExitLoop EndIf ; Note the point at which the next search should start $iOffset = @extended ; Now move back to the comma at the beginning of the word or the start of the string $iStart = $iOffset + 1 While 1 $iStart -= 1 If $iStart = 0 Or StringMid($sCSV, $iStart, 1) = "," Then $iStart += 1 ExitLoop EndIf WEnd ; And now forward to the comma at the end of the word or the end of the string $iEnd = $iOffset - 1 While 1 $iEnd += 1 If $iEnd > StringLen($sCSV) Or StringMid($sCSV, $iEnd, 1) = "," Then $iEnd -= 1 ExitLoop EndIf WEnd ; Add to list $aListing[0][0] += 1 ReDim $aListing[$aListing[0][0] + 1][3] $aListing[$aListing[0][0]][0] = StringMid($sCSV, $iStart, $iEnd - $iStart + 1) $aListing[$aListing[0][0]][1] = $iStart $aListing[$aListing[0][0]][2] = $iEnd WEnd _ArrayDisplay($aListing) Please ask if anything is unclear. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
richietheprogrammer Posted April 5, 2012 Author Share Posted April 5, 2012 Thank you both for your replies. Melba23, your example works, but when I replace $sCSV = "Autoit,Autarky,automatic,not_matched,Autumnal" with Global $sCSV _FileReadToArray("CILIST.csv",$sCSV) I get an empty Arraydisplay . Any ideas? Thanks again for your help. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 5, 2012 Moderators Share Posted April 5, 2012 richietheprogrammer,Just use FileRead - you want the whole file in one variable not in an array. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
richietheprogrammer Posted April 6, 2012 Author Share Posted April 6, 2012 Wow, I feel dumb, not sure why I thought that was an array variable. Thank you! It works now, except one thing, which Im guessing has to do with the regexp code. Basically, if the found item is the last item in that cell, the script grabs all the data after it, including the commas. Example: If I search "black" and in the csv file, there is "black color" , "color black" , and "irrelevant" seperate by commas (for csv), then the results will be: -1- black color -2- color black, irrelevant, and whatever might be after irrelevant.. Any idea how I can fix that? I found a temporary fix by adding a space before the commas, however that is only temporary and will cause problems with other functions. Thoughts? Thanks again! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 6, 2012 Moderators Share Posted April 6, 2012 richietheprogrammer, Try changing the <<<<<<<<<<<<<<<<<<<<< line: expandcollapse popup#include <Array.au3> ; Declare an array to hold the instances Global $aListing[1][3] =[[0, 0, 0]] ; Simulate a CSV file $sCSV = "black colour,colour black,black cat,not_matched,blackbird,shoeblack" ; Define the test string to search for $sTest = "black" ; Start looking $iOffset = 1 While 1 ; Find next instance of the test string StringRegExp($sCSV, "(?i)" & $sTest, 1, $iOffset) If @error Then ExitLoop EndIf ; Note the point at which the next search should start $iOffset = @extended ; Now move back to the comma at the beginning of the word or the start of the string $iStart = $iOffset ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< While 1 $iStart -= 1 If $iStart = 0 Or StringMid($sCSV, $iStart, 1) = "," Then $iStart += 1 ExitLoop EndIf WEnd ; And now forward to the comma at the end of the word or the end of the string $iEnd = $iOffset - 1 While 1 $iEnd += 1 If $iEnd > StringLen($sCSV) Or StringMid($sCSV, $iEnd, 1) = "," Then $iEnd -= 1 ExitLoop EndIf WEnd ; Add to list $aListing[0][0] += 1 ReDim $aListing[$aListing[0][0] + 1][3] $aListing[$aListing[0][0]][0] = StringMid($sCSV, $iStart, $iEnd - $iStart + 1) $aListing[$aListing[0][0]][1] = $iStart $aListing[$aListing[0][0]][2] = $iEnd WEnd _ArrayDisplay($aListing) That works for me. M23 richietheprogrammer 1 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
richietheprogrammer Posted April 9, 2012 Author Share Posted April 9, 2012 Thank you very much! I am not interested to know the total amount of results that are found, so how can I have it not show the first item in ArrayDisplay (The number of results found)? Or is that something within the code of _ArrayDisplay? Thanks again for all the help!! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 9, 2012 Moderators Share Posted April 9, 2012 richietheprogrammer, It is hardcoded into the _ArrayDisplay function. You will need to write your own function based on the original or a completely new one. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now