Editing
Squirrel Data Types
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
<div style="width:33.333%; z-index: 1; top: 0px; position: sticky; height: 30px; display: inline-block; text-align: center; line-height: 30px; background-color:#BBBBBB;">[https://sigwiki.potato.tf/index.php/Squirrel_Comments BACK]</div><div style="width:33.333%; z-index: 1; top: 0px; position: sticky; height: 30px; display: inline-block; text-align:center; line-height: 30px; background-color:#BBBBBB;">[https://sigwiki.potato.tf/index.php/An_Introduction_to_Squirrel HOME]</div><div style="width:33.333%; z-index: 1; top: 0px; position: sticky; height: 30px; display: inline-block; text-align:center; line-height: 30px; background-color:#BBBBBB;">[https://sigwiki.potato.tf/index.php/Squirrel_Operators NEXT]</div> Data stored in variables can have a number of different types which determines what type of object it is and what you can do with it. Below is the full list of data types in Squirrel, however we will only go over the primitive ones in this section. The other data types will receive their own sections later in the guide. '''Data Types:''' * Null * Integer * Bool * Float * String * Table * Array * Function * Generator * Class * Instance * Thread * Userdata To get a variable's data type as a string (see below on strings), you can use the <code>'''typeof'''</code> operator. <syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;> local t = typeof 5; // "integer" </syntaxhighlight> <hr> <span style="font-size:120%">'''Null'''</span> Has one value: <code>'''null'''</code>, it represents the non-existence or lack of a value. <syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;> local x; // null local y = 5; // ... y = null; </syntaxhighlight> <hr> <span style="font-size:120%">'''Integer'''</span> Represents a 32 bit whole number, meaning it can store any value from -2,147,483,648 to 2,147,483,647. You can specify the integer base as [https://en.wikipedia.org/wiki/Hexadecimal hexadecimal] by prefixing with <code>'''0x'''</code> or [https://en.wikipedia.org/wiki/Octal octal] with <code>'''0'''</code>. You can also specify [https://www.asciitable.com/ ASCII] char codes between single quotes '''<code>'</code>''' which will be stored as their integer value. <syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;> local x = 12345; // Decimal local y = 0xFFFFFF; // Hexadecimal begins with 0x local z = 050; // Octal begins with 0 local c = 'a'; // Char code stored as 97 </syntaxhighlight> These are all integer '''literals''', which are hard coded pieces of data within a program for a specific data type. Other types have literals as well which you'll see in the examples below. <hr> <span style="font-size:120%">'''Bool'''</span> Has two values: <code>'''true'''</code> and <code>'''false'''</code>. <syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;> local is_stuck = false; local should_round = true; </syntaxhighlight> <hr> <span style="font-size:120%">'''Float'''</span> Represents a 32 bit floating point number, meaning it can store any decimal value from 1.18e-38 to 3.4e38. Due to how floating point numbers are stored in memory, they have a limited amount of accuracy for a certain number of significant digits, for a 32 bit float it's usually around 6 or 7 digits. Any additional digits for the float value may result in rounding errors and precision loss. Keep this in mind when you're working with floats. <syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;> local float1 = 1.23456; // 1.23456 (Regular float literal) local float2 = 1.2e34; // 1.2e+34 (Scientific notation float literal) // Precision loss local float3 = 1.234567; // 1.23457 local float4 = 1234.567; // 1234.57 local float5 = 123456.7; // 123457 local float6 = 12345678999.0; // 1.23457e+10 </syntaxhighlight> You cannot have a leading floating point for float literals, they must start with at least one digit. <syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;> // Invalid local float1 = .123; local float2 = .12E5; // Valid local float3 = 0.123; local float4 = 0.12E5; </syntaxhighlight> <hr> <span style="font-size:120%">'''String'''</span> Strings are a sequence of characters with any length between quotation marks <code>"</code>. Strings are immutable, meaning they cannot be modified and you must create a new string if you need to change the old one. Regular strings must begin and end on the same line, they cannot contain any new line characters (\n). <syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;> // Strings ... local str1 = "The quick brown fox jumps over the lazy dog."; // String local str2 = "123"; // String with number characters, not an integer local str3 = "a"; // String local str4 = ""; // Empty string // ... compared to char codes local int1 = 'a'; // NOT a string local int2 = ''; // Compile error local int3 = '''; // Compile error local int4 = '\''; // 39, Valid char code for ' (explained below) // Need to make a new string if we want to modify local name = "bar"; name = "barber"; </syntaxhighlight> Strings (and char literals) may contain [https://en.cppreference.com/w/c/language/escape escape sequences], which are special characters that denote that something special should happen. Even though they have multiple characters in their definition, they count as one character when actually in use. Here are the ones you're most likely to use: * <code>'''\t'''</code> - Display a tab * <code>'''\n'''</code> - Move to a new line * <code>'''\\'''</code> - Insert \ char * <code>'''\"'''</code> - Insert " char * <code>'''\''''</code> - Insert ' char <syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;> local text = "Hello there! Please enter your class:\t"; // Tab local text2 = "What do you mean you don't \"have one\"?\n"; // " char and new line </syntaxhighlight> There may come a time when you need a string that does not interpret escape sequences or that can span multiple lines, which is where verbatim strings come in handy. Verbatim strings begin with <code>'''@"'''</code> and end with a regular quote <code>'''"'''</code>. <syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;> local mystring = @"eenie meenie minie moe"; </syntaxhighlight> You may also need to add quote characters within the verbatim string, to do so you double up each quotation <code>'''""'''</code>. The double quote will be replaced with a single quote by the compiler. <syntaxhighlight lang="c#" line="1" start="1" style="font-weight:bold;> local mystring = @"eenie ""meenie"" minie moe"; </syntaxhighlight>
Summary:
Please note that all contributions to SigMod are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
SigMod:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
British English
Views
Read
Edit
Edit source
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information