Forum > Beginners

Dimensioning the Dynamic Arrays

(1/4) > >>

ArtLogi:
Hello I encountered a beginners problem.

How do I increase ( or decrease ) the dimensions of dynamic array of type.  This is the main question.
So how I can declare dynamically:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Array(One dimensionsional of certain length)to Nth dÃ­mension array.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Array(first dimension of certain length, second dimension of certain length, Nth dimension of certain length)
The use is typical basic implementation of loading a data set from a typical textfile (to allow easy user editing ie. in excel and just for education for me)

FPC wiki does give example of setting length of list (1 dimensional array), but speaks nothing about adding dimensions and length of new dimensions. What I tried to look on the RTL and FCL there is no SetDimensions type of function. I assume this is piece of cake, but how you do it.  :)

There seems to be a logic inconsistency in general, since the FPC mixes 1 and 0 based indexing depending on the function. :(

Cyrax:
Use SetLength to set dimension of dynamic array. SetLength(DynamicArray, n, n, ..) for nth dimension.

ArtLogi:
Thx... I'm still making some mistake on my test code. With comments it works.

This results without comments: "Error: Wrong number of parameters specified for call to "SetLength"" in

--- Quote --- FreePascal IDE for Win32 for i386
Target CPU: i386
Version 1.0.12 2014/03/06
(Compiler Version 2.6.4)
(Debugger GDB 7.4)
Copyright (C) 1998-2012 by
--- End quote ---

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program arrytest;{\$mode objfpc} var        arry: array of string;begin        setlength(arry,4{,1});        arry[3]:= 'three - element four';        arry[0]:= 'zero - first element';        {arry[0,0]:= 'zero, zero'}        writeln('length(arry,2) and call arry(3)',arry[3]{,arry[0,0]});        readln;end.Also adding function call below leads to error? What I understood it should be a part of RTLs System unit.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---DynArrayDim(arry)it should return integer, but it doesn't matter if I pipe the result to integer variable or try to use it inside the writeln command, compiler still gives an error "DynArrayDim not found" or something on those lines.

I'm a bit confused.

FTurtle:

--- Quote from: ArtLogi on January 15, 2016, 03:12:23 pm ---How do I increase ( or decrease ) the dimensions of dynamic array of type.
--- End quote ---

Number of dimensions for dynamic array sets once at declaration type or variable and cannot be changed at runtime.

Leledumbo:

--- Quote from: ArtLogi on January 15, 2016, 03:12:23 pm ---There seems to be a logic inconsistency in general, since the FPC mixes 1 and 0 based indexing depending on the function. :(

--- End quote ---
There was NEVER 1 based indexing on arrays, only 0 based and up-to-you based ones.

--- Quote from: ArtLogi on January 15, 2016, 05:03:18 pm ---Thx... I'm still making some mistake on my test code. With comments it works.

--- End quote ---
You call SetLength with as many parameters corresponding to the number of dimensions as well. You only declare `array of string`, hence you can only set its first dimension. If you declare it as `array of array of string` you can set first and second dimension. More dimensions is the same.

--- Quote from: ArtLogi on January 15, 2016, 05:03:18 pm ---Also adding function call below leads to error? What I understood it should be a part of RTLs System unit.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---DynArrayDim(arry)it should return integer, but it doesn't matter if I pipe the result to integer variable or try to use it inside the writeln command, compiler still gives an error "DynArrayDim not found" or something on those lines.

I'm a bit confused.

--- End quote ---
Works fine:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  typinfo; type  tintdynarr = array of array of integer; var  a: tintdynarr;begin  writeln(dynarraydim(typeinfo(a))); // displays 2end. Please read the description in documentation before calling any function, it clearly states the parameter is a type info of a dynamic array.

Navigation

[#] Next page

Go to full version