|
Variables and Data TypesPerl has three different variables: scalar, array, and associative array. For some "strongly typed" languages such as C or Fortran, each variable must be explicitly declared before you use them. For C, it works like this: int i; i=67;But for Perl, the variables do not need to be decalred before their use. You can use them directly. The intepreter can recognize each variable by its prefix. Scalar variables can be a number, a string or undefined. Scalar varaibles are prefixed by $ sign. Here are some scalar variable example: $str1="www"; #assign string "www" to scalar variable $str1 $str2="cgi"; #assign string "cgi" to scalar variable $str2 $str3=$str1.$str2; #concatenate two strings so $str3="wwwcgi" $int=5; #assign integer 5 to scalar variable $int $int=3+2; #here $int=5 $int=3*4; #here $int=12 $int=5;$int++; #here $int=5+1=6 $int=5;$int+=4; #here $int=5+4=9
@name1=("viky","jeff"); #assign "viky","jeff" two strings to array @name1 @name2=@name1; #now @name2=("viky","jeff") @name3=("john",@name1); #now @name3=("john","viky","jeff") ($one,@name4)=@name3; #now @one="john" ,@name4=("viky","jeff") @name1=(); #now @name1 becomes an empty array @int=(2,6,7,8,9); #Five numbers are assigned to array @int $x=@int; #assign an array to an scalar variable will return the #number of elements in the array, so $x=5 $x=$#int; # $# is a special variable which will return the index of the last element in an array, so $x=4 ($x)=@int #$x is equal to the first element of array, so $x=2 $b=$int[0]; #$b is equal to the first element in array, so $b=2 $c=@int[0]; #$c is also equal to the first element in array, so $c=2 # $int[0] works the same way as @int[0] $int[0]=5; #assign 5 to be the first element of array, so @int=(5,6,7,8,9) $int[0,1]=[1,3] #asisgn 1 to the first element, 3 to be the second element, so @int=(1,3,7,8,9) @int[0,1]=@int[1,0] #the first two element exchange ,now @int=(3,1,7,8,9) @data=@int[0,1] #here @data=(1,3) $int[5]=10 # assigns the sixth element to @int so @int=(1,3,7,8,9,10) Associative arrays, sometimes called "hashes", are arrays that are indexed not on ordered integers, but on arbitrary string values. Typically, elements of an associative array are referred to as "key" and "value" pairs - the "key" is used to find the element of the array that has the "value". An associate array is prefixed with the percent(%) sign. Here is the format for an associate array: %arrayname=(key1,value1,key2,value2,key3,value3...);For each key in asscoiate array, there is a value corresponding to it:
Here are some associate array examples: %name=('john','biology','jeff','physics'); # suppose john teaches biology and jeff teaches physics $a=$name{'john'}; # here $a='Biology' $b=$name{'jeff'}; # here $b='physics' $name{'bill'}='math'; # add a new pair of element : key is bill, its value is math. # now %name=('john','biology','jeff','physics','bill','math') $name{'bill'}='English'; # Because we have already have a key 'bill' in the associate array, # so the 'bill' value change to 'English' # now %name=('john','biology','jeff','physics','bill','English') delete $name{'jeff'}; # now #now %name=('john','biology','bill','English') @X=%name # associate array assign to array @X %name=@X # array assign to associate array. |