Menu

#50 assigning an array (fill)

closed
nobody
None
5
2016-09-09
2016-08-08
No

Hello,

I'm glad you felt the need to add a function to fill an array in 1.99.99.56 - [r848].
This was the problem of most older programs.

(below are some of my old notes)
My proposal is to use instead of a new function this method:

fill array_name with value

array_name = ""
array_name = 0

or

array_name[] = ""
array_name[] = 0

Now we got:

1) Declare an empty array:
dim array_name(2,3)
dim array_name[2,3]

2) Declare and assign an array by using a list of elements (one by one element)
array_name = {1,2,3,4,5,6}
array_name = {{1,2,3},{4,5,6}}
array_name[] = {1,2,3,4,5,6}
array_name[] = {{1,2,3},{4,5,6}}

It will be nice to have also:

3) Fill an array (assign one element to entire array)
array_name = ""
array_name = 0
array_name[] = ""
array_name[] = 0

4) Assigning an array from another array
array_name = {1,2,3,4,5,6}
new_array = array_name #redim and fill from source

So, ( array[] and array are the same thing)

array[] = int      #fill array with int
array[] = float    #fill array with float
array[] = string   #fill array with string
array[] = array[]  #redim array and fill from second array
array[] = {list}   #redim array and fill from list

or in conjunction with dim or redim:

dim array(x) = int      #dim/redim fill array with int
dim array(x) = float    #dim/redim fill array with float
dim array(x) = string   #dim/redim fill array with string
dim array(x) = array[]  #dim/redim redim array and fill from second array
dim array(x) = {list}   #dim/redim array and fill from list

Eg.

dim a(10)
a=""               #or a[]="" to fill array with ""
dim b(20)
b[]=0              #or b="" to fill array with 0
a=b                #clear a array, redim a(20) and copy content of b into a
                                      #is the same as dim a=b

dim c(2,3)=""      #complex: create an array and fill it
dim z(5,8)=0.1
z=c                #clear z array content and make a copy of c array

c=x                #if x is int/float/string create an array and fill it
                                      #if x is array then clear c array content and make a copy of x array

                                      #ERRORS
dim c(2,3)={1,2,3} #error: declared dimensions do not match those provided
dim x(10,10)
dim c(2,3)=x       #error: declared dimensions do not match those provided
dim c=x

Not a bad move addition of fill function, but I think that filling an array coud be done more intuitively.
I also believe you should avoid adding new functions when possible. Their removal future will create more compatibility issues.
Its up to you.

PS Anyway, you did a great job with the latest upgrades. The code is more logical and you have eliminated a lot of dummy DataElement creation on errors. And the code is much, much faster!
Great... great job!

Related

Commit: [r848]

Discussion

  • Jim Reneau

    Jim Reneau - 2016-08-09
     
  • Jim Reneau

    Jim Reneau - 2016-08-09

    in 1.99.99.57

    I totally agree that it would have been nice to use the "=" sign to fill an array but the parser does not know if a variable is an array name or if we should use the value in the variable.

    Now we got:

    1) Declare an empty array:
    dim array_name(2,3)
    dim array_name[2,3]
    
    2) Declare and assign an array by using a list of elements (one by one element)
    array_name = {1,2,3,4,5,6}
    array_name = {{1,2,3},{4,5,6}}
    array_name[] = {1,2,3,4,5,6}
    array_name[] = {{1,2,3},{4,5,6}}
    dim array_name = {1,2,3,4,5,6}
    dim array_name = {{1,2,3},{4,5,6}}
    dim array_name[] = {1,2,3,4,5,6}
    dim array_name[] = {{1,2,3},{4,5,6}}
    
    3) Fill an existing array (assign one element to entire array)
    dim array_name fill ""
    dim array_name fill 0
    dim array_name[] fill ""
    dim array_name[] fill 0
    array_name fill ""
    array_name fill 0
    array_name[] fill ""
    array_name[] fill 0
    
    array_name = "" would change the array to a regular variable and store a single value.
    
    3+) create a new array and fill
    
    dim array_name(2,3) fill 0
    dim array_name[2,3] fill "snot"
    
    4) Assigning an array from another array
    dim new_array = array_name #redim and fill from source
    dim new_array[] = array_name #redim and fill from source
    

    I would like to get rid of the empty bracket notation [] but...

    This might not be exactly what you were thinking, but it will work well and was easy to make the lexer and interperter handle.

     
  • Florin Oprea

    Florin Oprea - 2016-08-10

    I took some time of thinking and I came to the following conclusions.
    (I will use only C-like notation: array[])

    You say:

    array_name = "" would change the array to a regular variable and store a single value.
    I am totally agree. Assigning a variable to another should be unitary as method and as expectations from the user.

    var1 = var2
    

    var1 (of any kind would have been) will become a copy of var2. Simple and clear.

    1) Declare DIMensions of an empty array and optionally fill with a single value:

    dim array_name[2,3]
    dim array_name[2,3] fill "snot" #this is from point 3+)
    

    2) Assign an array by using a list of elements and dimensioned array on the fly according to the list provided:

    array_name[] = {1,2,3,4,5,6}
    array_name[] = {{1,2,3},{4,5,6}}
    

    I have doubts that those two lines below should be accepted in conjunction with DIM command. DIM means declaring DIMensions and we declare none:

    dim array_name[] = {1,2,3,4,5,6}
    dim array_name[] = {{1,2,3},{4,5,6}}
    

    This is just an assignment
    A true DIM command should be like this:

    dim array_name[6] = {1,2,3,4,5,6}
    

    but this could generate others errors (dimensions != list elements). Eg.:

    dim array_name[2] = {{1,2,3},{4,5,6}}
    

    3) Fill an existing array (assign one element to entire array)

    array_name[] fill ""
    

    But I don't think those lines below should work

    dim array_name[] fill ""
    dim array_name fill ""
    

    DIM means "declare DIMensions for the array" So, If we translate this command

    dim array_name[] fill ""
    

    it will sound like this:
    I declare DIMensions for the array... with unknow size and fill it with ""

    4) Assigning an array from another array

    dim new_array[] = array_name #redim and fill from source
    

    See point 2
    This is a pure assign operation. This should be just: var1=var2


    Results:

    dim array_name[2,3]              #create an empty array by declaring DIMensions
    dim array_name[2,3] fill "snot"  #create an empty array and fill with a single value
    
    array_name[] = {1,2,3,4,5,6}     #assign an array by using a list of elements
    array_name[] = {{1,2,3},{4,5,6}} #assign an array by using a list of elements
    
    array_name[] fill ""             #fill an existing array
    
    array_one[] = array_two[]        #assigning an array from another array
    

    +just an opinion
    Instead of FILL would not look better <<?

    dim array_name[2,3] << "snot"  #create an empty array and fill with a single value
    array_name[] << ""             #fill an existing array
    

    Speaking of square brackets, the idea is to give the user the ability to use C style when it comes to arrays.

    Standard style:

    dim x(2,3)
    a = {1,2,3,4,5,6}
    b = {{1,2,3},{4,5,6}}
    c = x[1,2]
    a[0,2]=x[0,1]
    

    C style:

    dim x[2][3]
    a[] = {1,2,3,4,5,6}
    b[] = {{1,2,3},{4,5,6}}
    c = x[1][2]
    a[0][2]=x[0][1]
    

    I added just a single line (554) for that in basicParse.y file to work.


    Back to arrays...
    I'm not sure of anything right now.
    I feel like there must be something simple.
    But I needed to say what conclusions I reached.

    I attach basicParse.y file.
    Solve only syntax like above and not the actual code.
    It is just an opinion.

    Respectfully,
    Florin Oprea

     
  • Jim Reneau

    Jim Reneau - 2016-09-09
    • status: open --> closed
     
  • Jim Reneau

    Jim Reneau - 2016-09-09

    1.99.99.67 added 2d array access by a[][] and length access by a[?][] and a[][?]

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.