Jump to content

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


 Share

Recommended Posts

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

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
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?

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

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

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 Guest
Link to comment
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.

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

Link to comment
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.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

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
Link to comment
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.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

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

 

Link to comment
Share on other sites

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 Guest
Link to comment
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.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
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.

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