Jump to content

Deleting the first element of multiple arrays


Recommended Posts

Hello all, i have about 20 arrays in my script and i need to delete the first element of each, i have the name of the arrays in another array (its used in another part of the script)  and i tried to passing the names to from that array to the _ArrayDelete function but nothing happens and i get an error code :1 which state that i'm trying to delete an array which is not an array

 

code example

#include <Array.au3>
global $allarrays[3] = ['array1','array2','array3']
global $array1[3] = ['1', '2', '3']
global $array2[3] = ['1', '2', '3']
global $array3[3] = ['1', '2', '3']

for $i = 0 to UBound($allarrays) - 1
    _ArrayDelete($allarrays[$i], 0)
Next

_ArrayDisplay($array1)

obviously i can just use  _arraydelete over and over however i'm looking for a more elegant way of doing this

Link to comment
Share on other sites

Instead of putting the names of the arrays into an array, you could put the actual arrays into the array.

 

#include <Array.au3>


example()

;==========================================================================
;
;==========================================================================
Func example()

    local $array1[3] = ['1', '2', '3']
    local $array2[3] = ['1', '2', '3']
    Local $array3[3] = ['1', '2', '3']
    Local $allarrays[3] = [$array1, $array2, $array3]

    for $i = 0 to UBound($allarrays) - 1
        _ArrayDelete($allarrays[$i], 0)
    Next

    _ArrayDisplay($allarrays[0])
EndFunc

 

Link to comment
Share on other sites

Names of your arrays are $array, not just array.

You can use this code...

#include <Array.au3>
global $allarrays[3] = ['$array1','$array2','$array3']
global $array1[3] = ['1', '2', '3']
global $array2[3] = ['1', '2', '3']
global $array3[3] = ['1', '2', '3']

for $i = 0 to UBound($allarrays) - 1
    Execute("_ArrayDelete(" & $allarrays[$i] & ", 0)")
Next

_ArrayDisplay($array1)

 

Edited by kaz
Link to comment
Share on other sites

If you are hard set on names rather than array references you'll need some extra overhead

As far as removing the first item of an array your best bet is to set it to "" or some other sentinel value and then ignore that value OR delete all 'Empty' sentinels in a big batch

there just isn't a way around shifting the array down otherwise

in the includes folder have a look inside array.au3 you might be able to eliminate some of the code and make your own arraydelete function

Edited by Bilgus
Execute()
Link to comment
Share on other sites

4 minutes ago, kaz said:

Names of your arrays are $array, not just array.

You can use this code...

#include <Array.au3>
global $allarrays[3] = ['array1','array2','array3']
global $array1[3] = ['1', '2', '3']
global $array2[3] = ['1', '2', '3']
global $array3[3] = ['1', '2', '3']

for $i = 0 to UBound($allarrays) - 1
    Execute("_ArrayDelete($" & $allarrays[$i] & ", 0)")
Next

_ArrayDisplay($array1)

 

Just add the $ to the Execute statement then

Link to comment
Share on other sites

1 hour ago, TheXman said:

Instead of putting the names of the arrays into an array, you could put the actual arrays into the array.

 

#include <Array.au3>


example()

;==========================================================================
;
;==========================================================================
Func example()

    local $array1[3] = ['1', '2', '3']
    local $array2[3] = ['1', '2', '3']
    Local $array3[3] = ['1', '2', '3']
    Local $allarrays[3] = [$array1, $array2, $array3]

    for $i = 0 to UBound($allarrays) - 1
        _ArrayDelete($allarrays[$i], 0)
    Next

    _ArrayDisplay($allarrays[0])
EndFunc

 

i was trying to avoid that because the sub arrays had different sizes and and i need to change some values individually and honestly i'm just a beginner and i don't know how to do that just yet. however this 

 

1 hour ago, kaz said:

Names of your arrays are $array, not just array.

You can use this code...

#include <Array.au3>
global $allarrays[3] = ['$array1','$array2','$array3']
global $array1[3] = ['1', '2', '3']
global $array2[3] = ['1', '2', '3']
global $array3[3] = ['1', '2', '3']

for $i = 0 to UBound($allarrays) - 1
    Execute("_ArrayDelete(" & $allarrays[$i] & ", 0)")
Next

_ArrayDisplay($array1)

 

worked for me. the whole problem was not the $ in the beginning of the names (i messed up when i wrote it here :P) but it was me trying to pass the execute function to the _arraydelete function and not the other way around. oops :sweating:

 

Link to comment
Share on other sites

you can do it without the loop, but i suppose it is technically the same number of arraydelete calls.... still fun to write

#include <Array.au3>
global $allarrays[3] = ['array1','array2','array3']
global $array1[3] = ['1', '2', '3']
global $array2[3] = ['1', '2', '3']
global $array3[3] = ['1', '2', '3']


execute(stringregexpreplace(_ArrayToString($allarrays , ",") , "(array.*?)(,|\z)" , "_ArrayDelete($" & "$1, 0) "))


_ArrayDisplay($array1 , "array1")
_ArrayDisplay($array2 , "array2")
_ArrayDisplay($array3 , "array3")

 

there are other options like arrayextract, so make sure to continue exploring

global $allarrays[3] = ['array1','array2','array3']
global $array1[3] = ['1', '2', '3']
global $array2[3] = ['1', '2', '3']
global $array3[3] = ['1', '2', '3']


for $i = 0 to ubound($allarrays) - 1

_ArrayDisplay(execute("_ArrayExtract(eval($allarrays[$i]) , 1)") , "xtract" & $allarrays[$i])

Next

 

 

Edited by iamtheky

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

Link to comment
Share on other sites

All this sounds for me like a couple of good solutions to an ill-posed problem.

Why do you need to remove the leading (or even the Nth) row in a number of distinct arrays in the first place?

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

On 3/23/2018 at 10:22 AM, jchd said:

All this sounds for me like a couple of good solutions to an ill-posed problem.

Why do you need to remove the leading (or even the Nth) row in a number of distinct arrays in the first place?

to put it in short words i have a function that takes about 20 variables the vales of each variable are stored in the arrays and the function takes the first vale of each array then returns a result and then it has to take the second set and so on till one of the arrays runs out of values(all of them are of different lengths). i know i could easily keep an index or something to see when the function has reached the end of the shortest array. however its critical that the function down NOT take the same set of values twice and the easiest way of ensuring that is by simply deleting the value :).

Link to comment
Share on other sites

Then declare a single array with 20 columns and the max number of rows you need. When you have to store an incomplete set of 20 values, just store NULL in the cells concerned, i.e. where you have no value. That's the meaning of NULL, merely saying "I don't know the value". Don't delete rows, keep a running index of the row to be processed next.

That will result in cleaner code garanteed to work correctly by avoiding extra complexity.

Finally if you have a significantly large volume of data to handle, you should look towards use of a database to keep it organized and safe. Of course the recommended RDBMS engine I'd recommend is SQLite, which is well supported from within AutoIt.

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

15 minutes ago, Bilgus said:

Why Not Store the progress in the first element

to make it really easy just store the count in the element and decrement each time you read??

$atest[0] = [3, "A","B","C"]

Func Read()

    if $aTest[0] > 0 Then
        ConsoleWrite($aTest[$aTest[0]] & @CRLF)
        $aTest[0] -= 1
    EndIf

EndFunc

 

i thought of that but there is one thing i forgot to mention in my previous comment. there will be elements moved from one array to another (moved not swapped) depending on the last run of the main function plus there are other parts of the script that keep adding elements to the end of each array and the main function will keep "eating" from the beginning till one of them runs out and currently all of this is solved using one _arraydelete and one UBound at the begging of the main function  and i really don't want to over complicate it (i'm just a beginner and i only started programming less than a year ago and started using autoit about 2 monthes ago)

Link to comment
Share on other sites

With these new details it's now clear that without putting some solid semantics there you'll end up with extreme spaghetti code.

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

12 minutes ago, jchd said:

With these new details it's now clear that without putting some solid semantics there you'll end up with extreme spaghetti code.

also i want to point out that although i said it take about 20 variable it actually sometimes takes more and something less depending on the file its processing so and the output of the scripting will effect (to some extent) the content of future files to process. this script has started as a weekend project to make my life a bit easier and its not really designed for heavy work for now the  multiple arrays approach is working flawlessly and till i need a version 2 of the script i think i'll just stick with what i have 

Edit1: also there is no point in trying to maintain the data since it will be worthless after it goes in the function

Edited by Abdulla060
forgot to mention something
Link to comment
Share on other sites

I think this thread is failing to properly recognize how awesome @iamtheky's no loop srer was, for how much he sux with pcre that is magic.

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

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

×
×
  • Create New...