Jump to content

Can you explain to me how this is working


RickB75
 Share

Recommended Posts

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 by RickB75
Link to comment
Share on other sites

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 by RickB75
Link to comment
Share on other sites

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 by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

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

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

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