Defining an array vector

4

I'm learning Pascal and am having basic syntax problems to define a vector-type variable.

Here's the statement:

var
x:array[0..225] of String;

I would like to assign the following values and as follows:

x := ('X0','V1','V2','V3'),
('X1','V1','V2,'V3'),
('X2','V1','V2','V3'),
('X3','V1','V2','V3');

The variable x can be accessed as x[0,1] being the same as x['X0',1] returning the value V1 , and so on.

How can I apply this in Pascal?

    
asked by anonymous 25.10.2014 / 22:41

1 answer

4

In Delphi / Pascal, using Array in this way x['X0',1] is not possible. You really only get access to the values through the index.

This for Array, creating specialized objects is something else.

What you want is an array, which is declared using Array as well, and declares itself as:

var
  x: array[0..255, 0..255] of array of string;

  // ou declarando um array dinâmico, que não inicia com espaços/tamanho de uso
  // e deve ser incrementado o tamnho para uso conforme necessário
  //
  // assim:
  z: array of array of string; // claro que em qualquer uma das declaraçõeso tipo pode
                               // ser um tipo qualquer, até mesmo record´s e objetos.

As for how to access, it should be the numeric index. Example:

// leitura
value := x[254, 254];

// atribuição
x[254, 254] := value;

In the global scope, and only in it, you can do the direct assignment as in this example:

var
  matriz: array[0..1, 0..1] of integer = ((1,2),(3,4));

On the dynamic array, it is important to know that the space / size assignment for use is done with the function SetLength .

// adicionar um espaço
SetLength(x, 255, 255);

// setar novos tamanhos
// 1. remover espaços
SetLength(x, 254, 254);

// 2. atribuindo mais espaços
SetLength(x, 400, 500);
    
25.10.2014 / 23:01