How to make a condition based on the multiplicity of elements of a vector in the lawtex language?

Asked

Viewed 71 times

2

I’d like to make a condition (if) based on the multiplicity of elements of a vector (foreach). However, I have no idea how. I ask for practical help on how to do this. It would be something like,

if (número de elementos IN vectorX é maior que 1) {
    print "isso"
}

To put it in context:

if (numero de autor IN vectorAutores é 1) {
    print "vem"
}
if (numero de autor IN vectorAutores é maior que 1) {
    print "vêm"
}

2 answers

1


It is unclear if you are trying (1) to print the |vectorX| has more than one element, or (2) if a specific author appears more than once in the vector.

If this is the first option, you can use tube of size(), and in that case the code would be: (ref: Tube Size)

if(|vectorX|.size() == 1) {
    print "vem"
}
else {
    print "vêm"
},

Of course for this case where the only thing you want to do is to print a different value for each condition, the Tube Printif is more appropriate and summarized, and would look like this:

print printIf(|vectorX|.size() == 1, "vem", "vêm"),

Now if it’s the second option, I need to understand better what the struct that you are using for your vector, to direct you the best response.

But let’s say your struct is something like:

struct[Test] {
    name = "Test"
    request = "Test"
    fields {
        +[numAutor]: Integer,
        +[a]: String,
        +[b]: Currency
    }
}

And the declaration of its vector +|vectorAutores|: Vector[Test]. In this case, if you want to know how many times the author with a specific code appears in the vector, you need to use the Tube Filter, to filter all vector elements with this specific number, save to another vector, say the vector +|filtrado|: Vector[Test] and see the amount of elements of that result, using the size():

|filtrado| = |vectorAutores|.filter([this.numAutor] == 1),

print printIf(|filtrado|.size() == 1, "vem", "vêm"),

0

The operation that returns the amount of elements within a vector is

|nomeDoVetor|.size()

Example:

Suppose the vector |vectorExemplo| with 4 elements.

So, |vectorEmployment|. size() equals 4.

That is, if you do the following operation

print(|vetorExemplo|.size())

will be printed on screen the number 4

To do mathematical operations, just do the normal operation, but using the |vectorExemplo|. size() instead of the number. Example still based on |vectorEmplo| with 4 elements:

if(|vetorExemplo|.size() == 4){
    print("O vetor tem 4 elementos!!!")
}

Browser other questions tagged

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