Nasal library functions: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
m (should probably be reviewed/merged with Nasal library and then deleted (with a redirect) ?)
(Blank and redirect to Nasal library)
 
Line 1: Line 1:
{{Merge|Nasal library}}
#REDIRECT [[Nasal library]]
{{Nasal Navigation}}
 
Please first see: http://plausible.org/nasal/lib.html
 
A better and more complete description of these functions can also be found at http://wiki.flightgear.org/Nasal_library and in $FG_ROOT/Docs
== size() ==
'''Usage:'''
 
size(''object'');
 
where ''object'' may be a ''string'', a ''vector'' or a ''hash''.
 
'''Return value'''
 
An intger equal to the number of characters in a ''string'' argument or the number of elements in a ''vector'' or ''hash''.
 
If the argument is a non-string scalar then the error '''object has no size()''' will be generated.
 
== keys() ==
 
== append() ==
 
== pop() ==
 
== setsize() ==
'''Usage:'''
 
setsize(vect, new_size)
 
where ''vect'' is an expression which evaluates to a vector and ''new_size'' is an expression that evaluates to a non-negative number.
 
'''Action:'''
 
Changes the number of elements in the vetor ''v'' to the value of s.
 
'''Return value:'''
 
The resized vector.
 
'''Notes'''
 
Elements that exist in both the original and the resized vector retain their values. When the vector is enlarged newly created elements are set to ''nil''. When the size is reduced the values of all elements beyond the new size are permanently lost.
 
The ''new_size'' argument may be any non-negative number. Non-integer values are rounded down.
 
The ''vect'' argument need not be a named variable. The expression:
 
var v1 = setsize([], n);
 
Is a convenient way to allocate an empty vector of a specific size.
 
== subvec() ==
 
Create a new vector comprising elements from a contiguous range of elements of an existing vector.
 
'''Usage:'''
 
subvec([''vect'', [''start'', [''count'']]])
 
<dl>
<dt>''vect''</dt>
<dd>The original vector</dd>
<dt>''start''</dt>
<dd>The zero-based index in the original vector of the first element to be copied.</dd>
<dt>''count''</dt>
<dd>The number of elements to copy. If this argument is omitted then ''size(vect) - start'' is assumed.</dd>
</dl>
 
'''Return value'''
 
A new vector containing the specified range from the original vector, or ''nil''.
 
'''Notes:'''
<ol>
<li>
Omitting the ''vect'' and ''start'' arguments is not an error (possibly it should be) but the return value is ''nil''.
</li>
<li>
A negative ''start'' argument ''is'' an error. This seems wrong. Perhaps the language designer could comment.
</li>
<li>
A value of ''start'' greater than ''size(vect)'' causes an error. A value equal to ''size(vect)'' returns an empty vector.
</li>
<li>
If the value of ''count'' is greater than ''size(vect) - start'' then it is ignored. That is, all elements from ''start'' to the end of ''vect'' are returned. If ''count'' is zero then an empty vector is returned. A negative value of ''count'' causes an error.
</ol>
 
Example :
 
<syntaxhighlight lang="nasal" enclose="div">
var FGRoot = getprop("/sim/fg-root");
var filename = "/Aircraft";
var path_files = directory(FGRoot ~ filename);
 
foreach(var key; path_files) {
print(key);
}
</syntaxhighlight>
returns : '''"." , ".." , "Generic", "Instruments", "Instruments-3d", "c172p", "ufo"'''
 
 
With subvec():
 
<syntaxhighlight lang="nasal" enclose="div">
var FGRoot = getprop("/sim/fg-root");
var filename = "/Aircraft";
var path_files = subvec(directory(FGRoot ~ filename),2);
 
foreach(var key; path_files) {
print(key);
}
</syntaxhighlight>
returns : '''"Generic", "Instruments", "Instruments-3d", "c172p", "ufo"'''
 
 
<syntaxhighlight lang="nasal" enclose="div">
var FGRoot = getprop("/sim/fg-root");
var filename = "/Aircraft";
var path_files = subvec(directory(FGRoot ~ filename),3);
 
foreach(var key; path_files) {
print(key);
}
</syntaxhighlight>
returns : '''"Instruments", "Instruments-3d", "c172p", "ufo"'''
 
== delete() ==
Remove an element from a hash.
 
'''Usage'''
 
delete(''hash'', ''key'')
 
Remove the element with key ''key'' from the hash ''hash''. If the key is not present then the hash is unaltered.
 
== int() ==
 
== num(str) ==
Convert string to number.
Returns nil if str is nil, empty string, string with no number.
 
== streq() ==
 
== cmp() ==
 
== substr() ==
 
== chr() ==
 
== contains() ==
 
== typeof() ==
 
== ghosttype(ghost) ==
Returns a string containing either a descriptive name as passed to ''naNewGhost'' or ''naNewGhost2'' in C/C++ while creating the ghost or a unique id (aka the pointer to the C/C++ naGhostType instance) if no name has been set.
 
== compile() ==
 
== call() ==
 
== die() ==
 
== sprintf() ==
 
== caller() ==
 
== closure() ==
 
== find() ==
 
== split() ==
 
== rand() ==
 
== bind() ==
 
== sort() ==
 
== id() ==
 
= math =
== Trigonometric_functions ==
'''Caution''': All trigonometric functions works with [https://en.wikipedia.org/wiki/Radian Radian]-values - not with Degree!
 
<u>To convert this</u>:<br>
radians = degree * math.pi / 180<br>
degree  = radians * 180 / math.pi
=== sin() ===
=== cos() ===
=== tan() ===
=== asin() ===
=== acos() ===
=== atan2() ===
== exp() ==
== ln() ==
== pow() ==
== sqrt() ==
== floor() ==
Returns the value rounded downward
 
Example:
<syntaxhighlight lang="nasal" enclose="div">
math.floor(1.2); # returns 1
math.floor(1.6); # returns 1
</syntaxhighlight>
 
== ceil() ==
Returns the value rounded upward
 
Example:
<syntaxhighlight lang="nasal" enclose="div">
math.ceil(1.2); # returns 2
math.ceil(1.6); # returns 2
</syntaxhighlight>
 
== fmod() ==
A '''modulo'''-operator. The [https://en.wikipedia.org/wiki/Modulo_operation modulo]-operation finds the remainder of division of one number by another (sometimes called modulus).
 
fmod(x , y) may be negative if x < 0, whereas mod() seems to guarantee that 0 ≤ mod(x,y) < y, according to tests made with x and y integers, y > 0, x positive or negative.
 
== clamp() ==
== periodic() ==
== round() ==
== pi ==
== e ==
 
= io =
 
* SEEK_SET
* SEEK_CUR
* SEEK_END
* stdin
* stdout
* stderr
 
== stat() ==
== directory() ==
returns the files in a given directory
 
e.g. chatter_list = directory( chatter_dir );
 
Example :
<syntaxhighlight lang="nasal" enclose="div">
var FGRoot = getprop("/sim/fg-root");
var filename = "/Aircraft";
var path_files = directory(FGRoot ~ filename);
 
foreach(var key; path_files) {
print(key);
}
</syntaxhighlight>
returns : 
'''"." , ".." , "Generic", "Instruments", "Instruments-3d", "c172p", "ufo"'''
 
To delete the "." and ".." see the subvec() function.
 
== open() ==
== read() ==
== readln() ==
== seek() ==
== tell() ==
== write() ==
== flush() ==
== close() ==
 
= utf8 =
== chstr() ==
== strc() ==
== substr() ==
== size() ==
== validate() ==

Latest revision as of 12:54, 20 April 2016

Redirect to: