本章描述 mel的數(shù)據(jù)類型它們之間的轉(zhuǎn)換:
1、變量;
2、常數(shù);
3、數(shù)據(jù)類型轉(zhuǎn)換;
4、限制。
1、變量
所有變量名以$開始。變量名不包括空格和特殊字符。你可以使用下劃線和數(shù)字作為變量名但開頭不能是數(shù)字。
識別大小寫,如$temp不同于$temp。
例:
int $radical7mark; // 有效
int heychief; // error: 開頭缺"$"
int $ nine; // error: 開頭不是"$"
int $_val_id___a99v_; // 有效
int $howdyya`ll; // error: 含有無效字符
int $1bill; // error: 開頭不能是數(shù)字
有以下五種變量類型:
類型 意義 例子
int 整數(shù) (...-2, -1, 0, 1, 2...)10, -5, 和 0
float 小數(shù) 392.6, 7.0, and -2.667
string 一個或更多的字符 "what's up, chief?"
vector 三個浮點數(shù) <<3,?7.7,?9.1>>
matrix 浮點數(shù)陣列 <<1.1, 2, 3; 6.7, 5, 4.9>>
以上類型除matrix外,都可以是一個陣列。如,一個三元素的整數(shù)陣列是一個跟一個的三個整數(shù)。
聲明和標(biāo)注變量
聲明一個變量是說明變量的名字和類型;標(biāo)注一個變量是給已生命的變量一個專有的值。下例是將聲明和標(biāo)注合為一步:
int $temp = 3;
float $temp = 222.222;
string $temp = "heya kid.";
vector $temp = <<1, 2.7, 3.2>>;
matrix $temp[2][3] = <<4.5, 1, 0.2; -13, 9911, 0.007>>;
當(dāng)生命矩陣變量時,必須包括二維陣列的尺寸。
下例說明對整型、浮點、字符串和矢量陣列型變量陣列的聲明和標(biāo)注:
int $temp[5] = {100, 1000, -70, 2, 9822};
float $temp[4] = {43.3, -10.7, 0, 82.5};
string $temp[3] = {"lord", "flies", "cool brown fox2."}; < p>
vector $temp[2] = {<<0, 0, 0>>, <<0.01, -2, 16>>};
如果一個變量被聲明但未被標(biāo)注,它的所有的值是0;字串變量則?quot; "。
float $temp; // 賦值: 0;
string $temp[3]; // 賦值: {"", "", ""};
vector $temp[2]; // 賦值: {<<0, 0, 0>>, <<0, 0, 0>>};
matrix $temp[3][2]; // 賦值: <<0, 0; 0, 0; 0, 0>>;
如果一個變量被聲明或者被使用而沒有定義它的類型,它被隱含聲明為將要賦值給它的那種類型。
$temp = 0.0; // 浮點數(shù)
string $temp[]; // 零元素字符串陣列
$trip = "heya buddy"; // 字符串
$rip = {1, 2, 3, 4}; // 四元素整型陣列
$lip = <<1, 2.1; 3, 4>>; // 2x2 矩陣
$flixp = $temp; // 零元素字符串陣列
注意值0.0是一個浮點數(shù),而一個0值是一個整型數(shù)。這決定了在隱含聲明時是產(chǎn)生一個浮點數(shù)還是產(chǎn)生一個整型數(shù)。
不建議使用隱含聲明,因為它不象變量的隱含聲明那樣的清楚。
保留字
mel的保留字可以是一個變量類型、控制邏輯或是表達一個值。以下是mel的保留字:
break case continue default do else
false float for global if in
int matrix no off on proc
return string switch true vector while
yes
數(shù)據(jù)類型關(guān)鍵字
int float vector string matrix
布爾常數(shù)關(guān)鍵字
yes no on off true false
流動控制關(guān)鍵字
if else for while do in break continue default switch case
其他關(guān)鍵字
global return source catch alias proc
保留字也區(qū)分大小寫。所以int是整型,int不是。實際上alias、source、catch 也是保留字,但它們起指令作用,
因此沒有被包括在上述表內(nèi)。
字符串
字符串可用"+"運算連接。
string $what = "whale";
string $title = "great" + " white " + $what;
這將使title變量的內(nèi)容為great white whale。
矢量
為尋址一個vector的各個成分,使用"."。
vector $los = <<1, 2, 7>>;
float $firstcomponent = $los.x; // 賦值為 1
float $secondcomponent = $los.y; // 賦值為 2
float $thirdcomponent = $los.z; // 賦值為 7
標(biāo)注矢量的成分:
vector $lock = <<7, -4, 9>>;
$lock = <<$lock.x, $lock.y, 3>>; // assigned <<7, -4, 3>>
但不能直接給一個成分標(biāo)數(shù)字
[1] [2] 下一頁
新聞熱點
疑難解答
圖片精選