Jump to content
Abdulla060

Deleting the first element of multiple arrays

Recommended Posts

Abdulla060

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

Share this post


Link to post
Share on other sites
TheXman

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

 

  • Like 1

Share this post


Link to post
Share on other sites
kaz

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
  • Like 2

Share this post


Link to post
Share on other sites
Bilgus

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()
  • Like 1

Share this post


Link to post
Share on other sites
Bilgus
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

Share this post


Link to post
Share on other sites
Abdulla060
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:

 

Share this post


Link to post
Share on other sites
iamtheky

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

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

Share this post


Link to post
Share on other sites
jchd

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?

  • Like 1

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)

Share this post


Link to post
Share on other sites
Abdulla060
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 :).

Share this post


Link to post
Share on other sites
Bilgus

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

 

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
Abdulla060
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)

Share this post


Link to post
Share on other sites
Bilgus

You have yourself a FIFO queue a Null (sentinel value) for blank items is the way to go

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
Bilgus

How big are these arrays getting during execution?

Share this post


Link to post
Share on other sites
Abdulla060
1 minute ago, Bilgus said:

How big are these arrays getting during execution?

i thing i reached 350-ish on one of them yesterday 

Share this post


Link to post
Share on other sites
Abdulla060
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

Share this post


Link to post
Share on other sites
iamtheky

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.


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

Share this post


Link to post
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

  • Similar Content

    • DynamicRookie
      By DynamicRookie
      Hey There!
       
      So, what i need to do is an app that can read text in a image (I.e. a png that has text saying "This is a png" and return the text to a variable)
      I'm pretty much a newbie on AutoIt, my purpose is doing that but i don't know any function that can

      Any help is much appreciated
    • santoshM
      By santoshM
      How can i exit from a procedure in auto
      Func test() if x=o then     return endif endFunc  
    • Valnurat
      By Valnurat
      Hi
      Small question.
      I trying to find all index in an array with value higher than 7.
      How can that be possible?
    • TrashBoat
      By TrashBoat
      Could someone help me create or give an idea of how to do a incrementing for loop that would do this: https://i.imgur.com/YFUt47H.gifv
      I'm having a hard time figuring it out :S
    • msd1994
      By msd1994
      I have a script that just adds some keyboard shortcuts for things like displaying the current song and artist, moving the window to the side so it won't pop up in my way, and play/pause, next song, previous song (these are the only 3 to still work since they don't need the window handle.)
      In some update recently, Spotify's window class swapped from "[CLASS:SpotifyMainWindow]" to "[CLASS:Chrome_WidgetWin_0]". Using the new class in my controls doesn't seem to work, I've tried getting the window handle from the process handle (_GetHwndFromPID($PID)) but that seems to fail as well.
      Does anybody have some idea of a way I could get this script working again?
       
      edit: seems like discord has the same window class name, so could be some issue with this? Still not sure of a way to solve the issue though, I added a function to get the handle of the active window and can just use that now, but it was able to find it on its own before on spotify startup or script startup which would be preferred.
       
      Thanks!
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.