TheDcoder

Is it a Good Idea?: A new way to indicate arrays

22 posts in this topic

#1 ·  Posted (edited)

Hello! :D

After a long time, I bring you yet another IIAGI topic :P. Today its about indicating(?) arrays... here is a visual example:

"This is an example for a string"
1337 ; An integer
13.37 ; Integer
["This", "is", "an", "example", "for", "an", "array"]

; All work except the Array's example :(

All the examples (each separated by a line) can be passed to a function or a variable without the help of a variable except the array example... That shouldn't be like that! We can use the way (which I just demonstrated above) in many ways, here is one way I can think of now:

Local $aInitialArray = [1, 2, 3]

_ArrayConcatenate($aInitialArray, [4, 5, 6])

 

I would love to see this implemented :D... I honestly don't see reason for this NOT to be implemented B).

Edited by TheDcoder
Corrected the example

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites



That's called array literal.

The problem is not that you don't see a reason why is that bad idea. The problem is that some other people don't see it.

Forget it, it's a bad idea.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Due to potential memory issues, arrays are often passed around using ByRef. I have no idea if this has anything to do with what trancexx is implying, but the syntax would be incompatible with several standard array functions (at least) in the current Array UDF.

Share this post


Link to post
Share on other sites
13 minutes ago, trancexx said:

it's a bad idea

why? I am totally clueless

2 minutes ago, czardas said:

Due to potential memory issues, arrays are often passed around using ByRef

Yes, I am aware of that and I tested for it in the past by passing an array made by a function:

#include <Array.au3>

Global $aArray[2] = [1, 2]

_ArrayDelete(_ArrayAdd($aArray, 3), 2)

(AU3Check does show an error "_ArrayDelete() called with Const or expression on ByRef-param(s)" but if you bypass AU3Check the script runs without any problems.)

8 minutes ago, czardas said:

but the syntax would be incompatible with several standard array functions

How?


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

1 hour ago, TheDcoder said:

How?

What is this actually meant to do?

#include <Array.au3>

Local $aInitialArray = [4, 5, 6]
_ArrayConcatenate([1, 2, 3], $aInitialArray)

The target can't be an array literal. What is the variable name associated with the target?

Edited by czardas

Share this post


Link to post
Share on other sites
#include <Array.au3>


_ArrayDisplay(a('This,is,an,example,for,an,array'))


Func a($sArray,$sDel = ',')
    Return StringSplit($sArray,$sDel,1)
EndFunc

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

24 minutes ago, czardas said:

@gil900 That will only work with strings.

Try this:

#include <Array.au3>


_ArrayDisplay(a('"string 1",5,1.5,"string 2"'))



Func a($sArray,$sDel = ',')
    $aTmp = StringSplit($sArray,$sDel,1)
    For $a = 1 To $aTmp[0]
        If Not $aTmp[$a] Then ContinueLoop
        If (StringLeft($aTmp[$a],1) = '"' And StringRight($aTmp[$a],1) = '"') Or _
            (StringLeft($aTmp[$a],1) = "'" And StringRight($aTmp[$a],1) = "'") Then
            $aTmp[$a] = StringTrimRight(StringTrimLeft($aTmp[$a],1),1)
        Else
            If StringIsDigit($aTmp[$a]) Or StringIsFloat($aTmp[$a]) Then _
            $aTmp[$a] = Number($aTmp[$a])
        EndIf

    Next
    Return $aTmp
EndFunc

I have not checked it thoroughly but it should work

 

Another option:

_ArrayDisplay(a('string 1,5,1.5,string 2'))


Func a($sArray,$sDel = ',')
    $aTmp = StringSplit($sArray,$sDel,1)
    For $a = 1 To $aTmp[0]
        If StringIsDigit($aTmp[$a]) Or StringIsFloat($aTmp[$a]) Then _
            $aTmp[$a] = Number($aTmp[$a])
    Next
    Return $aTmp
EndFunc

 

Edited by gil900

Share this post


Link to post
Share on other sites

And they are UDFs, you can modify them to handle any type of garbage input you want.  And without a scope your thing is closer to a string without quotes, that just happens to be bracketed.


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

Share this post


Link to post
Share on other sites

@JohnOne True, it is possible to do in python...

@czardas That example was meant to demonstrate that ByRef works with non-variable objects.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

14 hours ago, TheDcoder said:

That example was meant to demonstrate that ByRef works with non-variable objects.

I didn't actually think it was such a bad idea, but I see absolutely no point in passing a literal string/array or whatever using ByRef: don't expect anything in return. Having said that, the functionality is not too difficult to mimic for cases where it actually might make sense. _ArrayConcatenate() is one of the worst examples you could have chosen. What did you expect as return value, [1,2,3,4,5,6]? How am I supposed to use that: I would still have to hard code it which means the function call must have been a complete waste of time. If the source parameter had been an array literal, the example would have been better.

Edited by czardas

Share this post


Link to post
Share on other sites

@czardas Ugh... I messed up with the example, I was sleepy and tried working on my project at that time... Fixed it now lol.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

I know why it is bad idea. you can't see why in Autoit but if you translate it to c++, you get code that use pointer for the "literal array". but when it gets the pointer, According to the book this pointer is not valid.. it point to memory address that still hold the array but the variable that was associated with it was deleted (out of scope).

I translated this code:
 

#AutoIt3Wrapper_Run_AU3Check=n

UseAndModifyArray(literalArray())

Func literalArray()
    Local $aiArray[3] = [-1,-2,-3]
    Return $aiArray
EndFunc



Func UseAndModifyArray(ByRef $aiArray)
    $aiArray[0] = 1
    $aiArray[1] = 2
    $aiArray[3] = 3
EndFunc

To this:

#include <Windows.h>
#include <iostream>
// Easy print macros:
#define pp std::cout <<
#define npp << std::endl <<
#define ee << std::endl;



int* literalArray(){
    int aiArray[3] = {-1,-2,-3};
    return aiArray;
}


void UseAndModifyArray(int* aiArray){
    aiArray[0] = 1;
    aiArray[1] = 2;
    aiArray[3] = 3;
}


int main(){
    
    UseAndModifyArray(literalArray());

    while (1){Sleep(100);}
    return 0;

}

 

what's happening in UseAndModifyArray(literalArray()) is:
1) Call to literalArray()
1.1) In literalArray() - declare some local Array -> return pointer to the local array
^ Here is the problem. the Array was local so when the function return, the array was "destroyed". By the book - Using this array after this is not valid. however, it still works because in low level the data of the array is still there in the memory and if you access to this memory address that returned from the function, you will get the data.

2) Call to UseAndModifyArray and give to it the memory address of the local array declared in literalArray.
2.1) access, read and modify the data using the memory address

 

Share this post


Link to post
Share on other sites

@gil900 Oh :o! So the pointer gets deleted and the array in the memory is open for overwriting. Am I correct?


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Something like that. What deleted is the variable that represents the memory address (Pointer that returned from literalArray()).
In theory, if you declare a new variable, The new variable may represents the same memory address because c++ know that this memory address is free for future allocations.

 

I'm not sure about this, but it seems logical to me. Fix it me if I'm wrong

Edited by gil900
1 person likes this

Share this post


Link to post
Share on other sites

Yup, That is what I told in in above post :D.

 

From what @gil900 pointed out, there is a risk that it (The Array Literal) might be overwritten in memory before it gets processed resulting in malfunction. So it makes implementing Array Literals more hard... but not that hard, here is a simple solution:

When the Interpreter parses a Array Literal, it will create a new temporary variable (pointer for the Array Literal) and deletes the variable (or pointer) after the completion of the operation (like evaluation of the expression or returning of the function).

This way we could avoid the accidental overwriting of the variable :).

 

P.S I am aware that implementing it programmatically would be difficult but IMHO its worth it.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

The solution is not difficult. In fact, the solution is very easy if you think about it. It does not have to be made in Autoit Core.

To solve it, the retunded Array must not be local. it must be global. this way the the variable will not deleted because autoit / c++ know that the variable is global.

Here is the solution:

#include <Array.au3>

Global $gg_aLiteralArrayTmp

_ArrayDisplay(a('"string 1",5,1.5,"string 2"'))



Func a($sArray,$sDel = ',')
    $gg_aLiteralArrayTmp = StringSplit($sArray,$sDel,1)
    For $a = 1 To $gg_aLiteralArrayTmp[0]
        If Not $gg_aLiteralArrayTmp[$a] Then ContinueLoop
        If (StringLeft($gg_aLiteralArrayTmp[$a],1) = '"' And StringRight($gg_aLiteralArrayTmp[$a],1) = '"') Or _
            (StringLeft($gg_aLiteralArrayTmp[$a],1) = "'" And StringRight($gg_aLiteralArrayTmp[$a],1) = "'") Then
            $gg_aLiteralArrayTmp[$a] = StringTrimRight(StringTrimLeft($gg_aLiteralArrayTmp[$a],1),1)
        Else
            If StringIsDigit($gg_aLiteralArrayTmp[$a]) Or StringIsFloat($gg_aLiteralArrayTmp[$a]) Then _
            $gg_aLiteralArrayTmp[$a] = Number($gg_aLiteralArrayTmp[$a])
        EndIf

    Next
    Return $gg_aLiteralArrayTmp
EndFunc

 

I just modified it a bit. now the the variable that represents the memory address will not deleted after the return because it is global.

Share this post


Link to post
Share on other sites

@gil900 I don't think it would be nice if it does not get deleted :sweating:


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

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

    • TheDcoder
      By TheDcoder
      Hello! I am back with another IIAGI? (Is it a Good Idea?)
       
      I think $CmdLineRaw is more like a macro than a "special" variable (which cannot be changed)... So making ti @CmdLineRaw would make more sense and there will 1 less exception to variables , I know that you were wondering about the $CmdLine "special" array... I didn't forgot it, but I don't think a macro can be a array, so I just ignored it... Maybe removing the $CmdLine and changing $CmdLineRaw to @CmdLine would be nice if you ask me .
       
      Think before choosing! TD
       
      P.S Feel free to disagree with me, I want hear your opinion as well .
    • TheDcoder
      By TheDcoder
      Hello , I was thinking of a situation where a key in a ini file can contain anything, If we were to know if the key does not exists using IniRead, Its not possible to do it without compromising a single possibility... I was thinking that if IniRead were to set @error when a key does not exist, it solves the problem which I mentioned before
       
      What do you think? Would you like this feature?
      TD