Jump to content

delete and recreating arrays


dove
 Share

Recommended Posts

Func _UpdateArrays()
    $selected_listview = StringReplace(GUICtrlRead(GUICtrlRead($ListView1)),'|', '', -1)
    $selected_transfered_fmt = _GetSelectedLine()

    _ArrayAdd($TransferedArray, $selected_transfered_fmt)

    ; copy $CsvImportArray data except value $selected_listview to $TempArray
    For $x = 1 To $CsvImportArray[0]
        $result = StringCompare($CsvImportArray[$x], $selected_listview)
        If $result <> 0 Then
            _ArrayAdd($TempArray, $CsvImportArray[$x])
        EndIf
    Next    

    ; copied $TempArray back to $CsvImportArray
    $CsvImportArray = 0
    Global $CsvImportArray[1]
    
    For $x = 1 To $TempArray[0]
        _ArrayAdd($CsvImportArray, $TempArray[$x])
    Next    
EndFunc

Please can someone suggest how an array can be recreated after being deleted. I want to copy array values excluding a certain value from one array to another tempoaray array then delete the source array. The temp array should then be copied back as the original array.

I have tried to implement this from the help file but I am having trouble as the attached code is not working. Can someone advise what is wrong?

A unique feature in AutoIt is the ability to copy arrays like this:

$mycopy = $myarray

To erase an array (maybe because it is a large global array and you want to free the memory), simply assign a single value to it:

$array = 0

This will free the array and convert it back to the single value of 0.

Link to comment
Share on other sites

I'm new to AutoIt, so please take this with a grain of salt, but are you looking for a function like this?

#Include <Array.au3>

Local $avArray[10]

$avArray[0] = 9
$avArray[1] = "Holger"
$avArray[2] = "Jon"
$avArray[3] = "Larry"
$avArray[4] = "Jeremy"
$avArray[5] = "Valik"
$avArray[6] = "Cyberslug"
$avArray[7] = "Nutster"
$avArray[8] = "JdeB"
$avArray[9] = "Tylo"

_ArrayDisplay($avArray, "Before")
Local $tempArray2 = _UpdateArrays($avArray, "Jeremy")
If Not @error Then
    $avArray = $tempArray2
    _ArrayDisplay($avArray, "after")
Else
    MsgBox(1, "Error", "Error in _UpdateArrays Method")
EndIf

Func _UpdateArrays($myArray, $stringToDelete)
    If _ArraySearch($myArray, $stringToDelete) = -1 Then
        SetError(1)
        return -1
    EndIf

    Local $tempArray[1]
    $tempArray[0] = 0
    For $i = 1 To $myArray[0]
        If StringCompare($myArray[$i], $stringToDelete) <> 0 Then
            _ArrayAdd($tempArray, $myArray[$i])
        EndIf
    Next
    $tempArray[0] = UBound($tempArray) - 1 ;; not sure about the -1 bit

    Return $tempArray

EndFunc
Link to comment
Share on other sites

Why not just delete the value from the original array in the first place, without messing around with the temp array:

For $x = $CsvImportArray[0] To 1 Step -1
    If StringCompare($CsvImportArray[$x], $selected_listview) = 0 Then  
        _ArrayDelete($CsvImportArray, $x)
        ExitLoop ; Quit looking (assumes there will only be one instance to delete)
    EndIf
Next

Take the ExitLoop out if there will be more than one instance to delete.

:(

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Why not just delete the value from the original array in the first place, without messing around with the temp array:

For $x = $CsvImportArray[0] To 1 Step -1
    If StringCompare($CsvImportArray[$x], $selected_listview) = 0 Then  
        _ArrayDelete($CsvImportArray, $x)
        ExitLoop ; Quit looking (assumes there will only be one instance to delete)
    EndIf
Next

Take the ExitLoop out if there will be more than one instance to delete.

:(

I not sure how if I had a problem with refreshing the item list whan I tried. But I may not of done correctly this looks good. Thanks
Link to comment
Share on other sites

Dove,

Welcome to the Forum!

Follow the Penguin advice, it's forcibly good!

Now observe that in an assignment, left-hand and right-hand values are independant. Saying that I mean that you are perfectly allowed --if not encouraged-- to avoid multiplicating variables where that can be avoided.

When you do:

Local $choice[2] = ["abc", "def"]

...

$choice = StringUpper($choice[1])

the last assignment means, litterally:

take the second item in array $choice

pass it to function StringUpper, which returns a string

assign the returned string value to variable $choice, destroying its previous content.

So after doing that, $choice is now a string containing "DEF". Of course, doing so requires that you are aware that $choice is no more an array and use it accordingly in subsequent code, but my point was that you don't have to use another variable in such case.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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