Skip to content

String handling

atecon edited this page Sep 26, 2024 · 1 revision

String concatenation with sequences

In Gretl (since version 2024b), users can combine string concatenation with sequence generation to create a series of numbered strings. The eval (or print) command, along with the tilde ~ operator and the seq() function, allows for this powerful combination.

Example:

eval "a " ~ seq(1,4)

This command produces the following output:

a1
a2
a3
a4

How it works:

  • The string "a " is concatenated with each element of the sequence.
  • seq(1,4) generates a sequence from 1 to 4.
  • The tilde ~ operator performs the concatenation.
  • Each resulting string is output on a new line.

This feature is particularly useful for creating numbered labels, variable names, or any situation where you need a sequence of similar strings with incrementing numbers.

The returned sequence can also be stored as a array of strings. Here an example:

string s = "a " ~ seq(1,4)
print s

which yields:

a1
a2
a3
a4
Clone this wiki locally