Data Type (Part 2 - Array/Hash Variable)-- Perl Study Notes
Array Variable
Array operator: ..
(1 .. 5) # same as (1,2,3,4,5)
(1.1 ..5.1) #same as (1.1,2.1,3.1,4.1,5.1)
(3.2 .. 5.1) #same as (3.2,4.2)
Notes: .. operator create a list of values starting at the left scalar value up through the right scalar value incrementing by one each time
Having the right scalar less than the left scalar results in an empty list
If the final value is not a whole number of steps above the initial value, the list stops just before the next value would have been outside the range,just as the above sample
Only effective to the number lists
Array example,
@fred=qw(4 5 6);
@fred=(4,5,6);
$a=@fred; # $a=3, If one array is assigned to a scalar variable, the number assigned is the length of the array.
($a)=@fred; $a=4, only assign the first value to the $a.
@fred=("fred", "wilma", "pebbles", "dino");
$fred[-1]; # return "dino"
$#fred; #return 3. $#array_name returns the last index of the array
Hash Variable
Hash example,
$fred{"aaa"} = "bbb"; # creates key "aaa", value "bbb"
$fred{234.5} = 456.7; # creates key "234.5", value 456.7
if %hash {...} # in the scalar context, merly using %hash will reveal whether the hash is empty or not
Hash Slices
@score{"fred","barney","dino"} = (205,195,30);
@players = qw(fred barney dino);
print "scores are: @score{@players}\n"; # scores are: 205 195 30
No comments:
Post a Comment