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

Whoever said that there's no such thing as a dumb question, probably asked a lot of DUMB questions.  Dumb questions are ones that you can easily find the answer to on your own.

Share this post


Link to post
Share on other sites
kaz
Posted (edited)

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
Posted (edited)

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
Posted (edited)

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
Posted (edited)
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

    • bmy007ro
      By bmy007ro
      I have this psexec command working fine 
      Local $sMachine = InputBox("Input PC name", "Enter Computer Name")
      psexec \\$sMachine -u $sDomain\$sUserName -p $sPassword  \\NetworkFLD\FILENAME.cmd > C:\Temp\TT\My.log
       
      I wanted to converted to AutoIT script
      Cloud you help, please !?
       
    • autotin
      By autotin
      Hello I am a first time user of AutoIT with little coding experience and I would REALLY really appreciate your help.
      I work in a warehouse where I am responsible for data entry along with a slew of other different tasks such as auditing and managing paperwork. Most of my time is tied up entering info to close receipts of product orders (8-12+ hours for thousands of products). The quantity actually produced by the warehouse is sometimes different from the quantity listed on the reciept (in this case 20). I need to change all the build qty for sublocation "1-WIP" from 20 to the actual quantity 22 and set all other numbers as 0. The form is an iframe coded with jQuery and freezes for 1-2 seconds after every entry and refreshes. As such, it doesn't allow me to use tab and I need to click and type every incorrect field.
      I have attached an image as an example from the webapp, FinaleInventory.
      I'm thinking that I probably need a macro that has a message box entry where I can type the corrected number entry I want. Then it uses image/character recognition (Tesseract?) to find "WIP" sublocation =,   Move the mouse over to the right to "Quantity" relative to location of WIP If quantity number = entry, then move to next "WIP" image (Not quite sure how to make Tesseract "find next") Elseif quantity number ≠ entry then click quantity number of "WIP" and type corrected entry Scroll down Loop until bottom of page I would greatly appreciate any help, ideas or pointers for this project. Thank you!
      -A newbie scripter

    • Pagi
      By Pagi
       
      <a href="index.php=4143b27b41db0a40c499202865c3e24d" class="menuitem " target="\_self"> "Menu Item 1" </a>
      Can you tell me if it´s possible to click this menu button? If yes, how? Cause I have no idea, I think i should use _IETagNameGetCollection
    • mar3011
      By mar3011
      hello, I want to set in the inputbox field random key from the keyboard to which the function will be assigned, for example F4 function start notepad ALT + A function start notepad for one function. Thank you for any help.  
    • JuanFelipe
      By JuanFelipe
      Cambiar a español Hello friends, I need help with a problem I have, I want to know if there is any way to update a GUI already running, that is I have a GUI of a certain size and with a button I would like to enlarge it:
      example: $ form1 = GUICreate ("", 200,100,0,0)   When you press the button, the gui increases its size, and actuates the new size.
×