Defining an array vector

Asked

Viewed 4,167 times

4

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

Here is the statement:

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

I would like to assign the following values and in the following way:

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?

1 answer

4


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

That for Array, creating specialized objects is something else.

What you want is a matrix, which declares itself with the use of Array also, and declares itself so:

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, I emphasize that it should be by the numerical index.
Example:

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

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

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

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

On the dynamic matrix, it is important to know that the assignment of spaces/size 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);
  • Thank you very much! But isn’t there another way of attribution? Can you define the 4 values for the matrix x[0]? For example: x[0]:='a','b','c',’d';

  • 2

    I managed to apply in my project, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.