perl6变量类型学习

来源:互联网 发布:守望先锋最新伤害数据 编辑:程序博客网 时间:2024/06/03 20:59

详见注释…

#Perl 6 variables are classified into 3 categories: Scalars, Arrays and Hashes.#A sigil (Sign in Latin) is a character that is used as a prefix to categorize variables.#--------------Scalars begins here----------#$ is used for scalars#Stringmy $name='John Doe';#a line of perl6 code must be ended with ';'say $name.uc;#to uppercasesay $name.chars;# number of charssay $name.flip;#reverse#Integermy $age=99;say $age.is-prime;#Both String and Integer start with $,#which means holds one value or referencemy $num = 2.3;say $num.numerator;#分子say $num.denominator;#最小分母say $num.nude;#--------------Scalars ends here----------#-------------- Arrays begins here----------#@ is used for arrays#Arrays are lists containing multiple values.my @animals = '123', 'cd', 'hg';#The array accept any number of values#thus,length is auto-extendingsay "the number of animals is"~@animals.elems~"animals";#The tilde ~ is used for string concatenationsay "The animals is: " ~ @animals;say "I want to add an element";@animals.push("hehe");#add element to an Arrayssay "Animal is:"~@animals;say "What I put in last is:"~@animals[3];@animals.pop;#delete last oe elementsay "We're going to remove: " ~ @animals.splice(1,2) ~ " and keep the " ~ @animals;#splice(a,b) will remove b elements starting at position a.my @test[3];#This array will be able to hold a maximum of 3 values, indexed from 0 to 2.@test[0]="hehe";@test[1]="xixi";@test[2]="haha";say @test;#Multidimensional arraysmy @tbl[3;2];#Think of it as a 3x2 grid or array.@tbl[0;0]=1;@tbl[0;1]="he";@tbl[1;0]=2;@tbl[1;1]="ha";@tbl[2;0]=3;@tbl[2;1]="hei";say @tbl;=podVisual representation of the array:[1 x][2 y][3 z]=cut#-------------- Arrays ends here----------#-------------- Hashes begins here----------=podA Hash is a set of Key/Value pairs.% is used for Hashes=cutmy %capital=('uk','London','China','beijing');my %capital_copy=("uk"=>"London","China"=>"beijing");say %capital==%capital_copy;#Truesay %capital;say %capital_copy;%capital.push: ("France" =>'Paris');#equals %capital.push: (France =>'Paris');#.push: (key ⇒ 'Value') adds a new key/value pair.say %capital.kv;say %capital.keys;say %capital.values;#-------------- Hashes ends here----------
0 0