RickB75 Posted February 3, 2016 Share Posted February 3, 2016 (edited) I found this short funtion here on the forums. It's works perfect for what I need. Perfect!! I was stumped with a simple task before I found this short script. Basically I needed to remove a line in a txt file if it contained " U ". I kept running into problems with the funtion _FileWriteToLine because my array kept changing sizes. That was before I found this function. Global $_Array _FileReadToArray ( 'C:\myfile.TXT', $_Array ) $_Array = _DeleteArrayElementWithStringInstr ( $_Array, " U " ) _FileWriteFromArray ( 'C:\myfile', $_Array, 1 ) Exit Func _DeleteArrayElementWithStringInstr ( $_Array, $_String ) Local $_Item For $_Element In $_Array If StringInStr ( $_Element, $_String ) <> 0 Then _ArrayDelete ( $_Array, $_Item ) Else $_Item+=1 EndIf Next Return ( $_Array ) EndFunc ;==> _DeleteArrayElementWithStringInstr ( ) Whats got me stumped is the $_Element var. I was under the assumption that $_Element var needs to be a counter of some sorts. Kinda like a "For...To...Next" loop. Looking in the help file, the StringInStr function needs to have the "String to evaluate" in the first parameter. How is the _$Element var getting the right string that I need which is " U ". I've always used a position in the array when using StringInStr like this: For $i = 0 To Ubound($array) -1 If StringInStr($array[$i], " U ") Then Msgbox(0,"Item found", "Item found on this Line: " & $i) Endif Next I'm assuming that $_Element is a counter of some sorts I think. I guess I just looking for a little more clarity. I've read the help file but I'm still a little confused. I guess I'm trying to figure out what is telling the script to increment to the next item in the array? Does the "For...In...Next loop automatically loop through all the elements with out a counter and the script is using $_Item as the position identifier to know what position that the loop is in. Edited February 3, 2016 by RickB75 Link to comment Share on other sites More sharing options...
AutoBert Posted February 3, 2016 Share Posted February 3, 2016 When deleting in array you must loop from end to begin. Link to comment Share on other sites More sharing options...
RickB75 Posted February 3, 2016 Author Share Posted February 3, 2016 (edited) Isn't $_Item incremented up though with += 1 ? I apologize if this is basic stuff. I'm just trying to get my head wrapped around this particular loop and how it's working. I thought you needed Ubound to get the total size of the array and then loop backwards Edited February 3, 2016 by RickB75 Link to comment Share on other sites More sharing options...
AutoBert Posted February 3, 2016 Share Posted February 3, 2016 When a element for example 9 is deleleted in a loop from 0 to Ubound(array)-1 all elements after where at new pos. (-1) Its easier to loop from Ubound(array)-1 to 0 Step -1. When element 9 is deleted elelments 0...8 are in same place as before. Link to comment Share on other sites More sharing options...
iamtheky Posted February 3, 2016 Share Posted February 3, 2016 (edited) For..In makes more sense with Maps and other datatypes that are not nicely numbered 0 through ubound. deleting things that way should be fine, though. If its going to be an array i would do it like so (well probably not, but it is a way): #include<array.au3> local $aArray[] = ["keep" , "U_no_keep" , "keep" , "U_no_keep"] _ArrayDisplay($aArray) $aTrimmedArray = _DeleteIfArrayContainsString($aArray , "U") _ArrayDisplay($aTrimmedArray) Func _DeleteIfArrayContainsString($Array , $sStr) $aMatches = _ArrayFindAll($Array , $sStr , 0 , 0 , 0 , 1) _ArrayInsert($aMatches , 0 , ubound($aMatches)) _ArrayDelete($Array , $aMatches) return $Array EndFunc Edited February 3, 2016 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
RickB75 Posted February 3, 2016 Author Share Posted February 3, 2016 I'm with ya now Bert!! Thanks for the explaination. I'm sure that's why my initial script was failing. I was starting from the beginning and I should have started from the end of the array and worked backwards. I'm assuming the "For...In...Next" loop automatically loops through each position in the array and doesn't take the array size into the equation. $_Item just tells the ArrayDelete function which position to delete in the array! Is that right? Kinda??? Link to comment Share on other sites More sharing options...
kylomas Posted February 4, 2016 Share Posted February 4, 2016 Yes...the areaydelete is handling the array navigation... Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
RickB75 Posted February 4, 2016 Author Share Posted February 4, 2016 Thanks Kylomas for confirming that for me. Also, a big thanks to each one of you guys taking time and replying to my question. It really helps. Link to comment Share on other sites More sharing options...
Malkey Posted February 4, 2016 Share Posted February 4, 2016 When using "ByRef", the original array is directly changed in the function. Therefore, another changed array need not be returned from the function. "ByRef should be used when passing large amounts of data .... " #include <Array.au3> Global $aArray = [1, "2 U ", 3, 4, 5, " 6 U ", "7"] _DeleteArrayElementWithStringInstr($aArray, " U ") _ArrayDisplay($aArray) Func _DeleteArrayElementWithStringInstr(ByRef $aArraySame, $sString) For $i = UBound($aArraySame) - 1 To 0 Step -1 If StringInStr($aArraySame[$i], $sString) Then _ArrayDelete($aArraySame, $i) EndIf Next EndFunc ;==>_DeleteArrayElementWithStringInstr 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