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

    • nooneclose
      By nooneclose
      Good morning everyone, I thought I had already solved this issue but it turns out I did not. 
      My code finds unread emails with this specific subject line of "request" but when I change the subject to SKIPPED + "request" = ("SKIPPED request") the program still finds the email and tries to process it. I only want to process emails with the exact match subject of "request".
      Here is my code that "finds" the unread emails with the subject of "request" or so I thought. 
      Func ListUnreadEmails() ;******************************************************************************* ; Lists all unread E-mails from the folder Outlook-UDF-Test ;******************************************************************************* ; Stores all the unRead emails into an array Global $aItems = _OL_ItemFind($oOutlook, "*\Outlook-UDF-Test", $olMail, _ "[UnRead]=True", "Subject", "request", "EntryID,Subject", "", 1) ; Displays the array of unRead emails If IsArray($aItems) Then ;_ArrayDisplay($aItems, "OutlookEX UDF: _OL_ItemFind - Unread mails") Else MsgBox(48, "OutlookEX UDF: _OL_ItemFind Example Script", _ "Could not find an unread mail. @error = " & @error & ", @extended: " & @extended) EndIf ; Gets the number of unread emails Global $numberOfUnRead = UBound($aItems, $UBOUND_ROWS) - 1 ;MsgBox("", "Number of Unread emails", $numberOfUnRead) EndFunc It acts as if any part of the subject containing the word "request" and the email is unread that it will try to process it. (I think)
    • KhalidAnsari
      By KhalidAnsari
      Hi,
      I need to automate Java application using AutoIt, I have refer forum for the same, as per forum I am using Java Access Bridge to finding control name. But could not set value to control. Below is my sample code 
      Send("#r")
      sleep(1500)
      Send("D:\javaapplication\javaapplication\Hello.jar{Enter}")
      WinActivate("Penjumlahan")
      sleep(1500)
      ControlSetText("Penjumlahan", "", "Name:  A :", "demo")
      Java Application and Java Ferret screen

    • QaisBsharat
      By QaisBsharat
      Hello, 
      May i know how can i automation qt application?
      Using controlclick not mouse and positions
       
      Thanks
    • nooneclose
      By nooneclose
      Good afternoon, I am in need of some help. I am sure this is a stupid question requiring only one or two lines of code. However, I would greatly appreciate the help I cannot figure this out. I also tried searching for the answer on the internet but no one except me apparently seems to be having a hard time figuring this out and or is asking about it. 
      I simply want to change the "status" of an email from unread to read once I have processed it. My code is over 500 lines and I would like not to clutter this post with it. Assume I have all my includes and connections properly defined and stuff. 
      Here is the bit of code where I am trying to change the email that was used from unread to read:
      Func ChangeEmailStatus() ;******************************************************************************* ; changes the status of an email from unread to Read ;******************************************************************************* Local $iRows = UBound($aItems, $UBOUND_ROWS) MsgBox("", "Number of Unread emails (Before Change)", $iRows) _OL_ItemModify($oOutlook,$aItems[$i][0], Default, "Read=True") MsgBox("", "Array Display 1", $aItems[1][0]) MsgBox("", "Array Display 2", $aItems[2][0]) Local $iRows = UBound($aItems, $UBOUND_ROWS) MsgBox("", "Number of Unread emails (After Change)", $iRows) EndFunc  
    • SharpDressedMan
      By SharpDressedMan
      Hello,
      Is there any way to check for validity of a pointer to a DllStruct ?
      In the above code, a DllStruct is created from an invalid pointer. How to prevent from doing this ?
      local $tStruct = DllStructCreate("int i") DllStructSetData($tStruct, "i", 123) local $pStruct = DllStructGetPtr($tStruct) $tStruct = 0 ; destroy DllStruct ==> $pStruct becomes invalid ; how to check here for validity of $pStruct and prevent from doing the following ? $tStruct = DllStructCreate("int i", $pStruct) ; create DllStruct from invalid pointer... MsgBox(0, @ScriptName, @error) ; displays '0' : invalid pointer is not detected by DllStructCreate() MsgBox(0, @ScriptName, IsDllStruct($tStruct)) ; displays '1' : tStruct is assumed as a valid DllStruct object, which is not Thanks for help
×