Variables

Variables and data types

Basic syntax

A variable identifier is composed by a '%' (percent) sign followed by a sequence of letters, digits or underscores.
Examples of valid variable names are:
%i,%variable,%MyVar,%1,%thisisavar,%2ndName,%_hidden

Local and global variables

Variables can be local or global.
Local variables preserve their contents only inside the scope of a single script.
Global variables are shared between all the scripts and preserve their contents until they are explicitly unset or until KVIrc quits.
By default KVIrc assumes that the variables you're referencing are local.
If you want to force the scope of a variable to global you need to pre-declared it with the global keyword.
    # copy this script to a file and run /parse <filename>
    global %a
    %a = "The contents of the variable a"
    %b = "The contents of the variable b"
    # %a is a global variable now : all the other scripts can see its value
    # %b is a local variable and no other scripts can see its value
One notable exception is the commandline. When you type the commands "manually" KVIrc assumes that the variables you're referencing are global. This is generally useful since you don't want to type 'global %x' before every command you try.
If you have executed the example above from a file (by the means of parse) then now you can type
    [cmd]echo %a[/cmd]
in the commandline to see the contents of the variable %a.
If you also try
    [cmd]echo %b[/cmd]
you will probably see nothing printed since %b was local to the parsed script.
Older KVIrc versions recognized global variables when they were starting with an uppercase letter and local ones when the initial letter was lowercase.
This method was incoherent since the other parts of the language are case insensitive.
The case sentive method is still supported for backward compatibility but is deprecated and the parser warns about it.

Variable types

KVS has three main data types: scalars, arrays of scalars and associative arrays of scalars (also known as hashes).
Scalars
The scalars are simple variables containing a single value (a string or an integer).
    # %a is a scalar variable
    %a = "This is a string"
    echo %a
    %a = 24.5
    echo %a
Arrays
The arrays are collections of items indexted by integers.
An easy way to create an array is to use the $array function.
    %a = $array("element1","element2","element3")
    for(%i=0;%i<3;%i++)
    {
        echo %a[%i];
    }
Any variable is a sort of variant and can assume different type identities in different times.
    # %a is a scalar
    %a = "This is a string"
    # %a becomes an array with 3 elements
    %a = $array("element1","element2","element3");
    # %a becomes a hash with two values
    %a = $hash("key1","value1","key2","value2");
Obscure KVIrc internals:
Internally KVS is an implicitly typed language: the "scalar" data type is in fact a set of types that KVIrc manages silently. Notable types are "integer","real" and "string".

Main, Language Overview
KVIrc 3.0.0 Documentation
Generated by dag at Wed Jul 7 23:12:51 2004