my $stuff = "hello word";
my $where = index($stuff, "wor");
print "where: $where\n";
my $where1 = index($stuff, "w");
print "where1: $where1\n";
my $where2 = index($stuff, "w", 6);
print "where2: $where2\n";
$substr1 = substr("hello world", 6, 5);
print "substr1: $substr1\n";
substr($substr1,0,0)="hello ";
print "substr1: $substr1\n";
$string = "hello world";
substr($string, 0, 5, "xxxxx");
print "string: $string\n";
$year = "2014";
$month = 8;
$day = 2.0;
$data_tag = sprintf("%s/%d/%.3f", $year, $month, $day);
print "data_tag: $data_tag\n";
sub by_num{
$a <=> $b
}
@nums = (1,4,22,5,33,10,12);
print "nums: @nums\n";
@result1 = sort @nums;
print "result1: @result1\n";
@result2 = sort by_num @nums;
print "result2: @result2\n";
my %socre = (
"kevin" => 100,
"xiang" => 50,
"jie" => 150,
"xxx" => 1,
"yyy" => 50
);
@socre1 = %socre;
print "socre1: @socre1\n";
sub by_socre_value{
$socre{$a} <=> $socre{$b}
}
@socre2 = sort by_socre_value keys %socre;
print "socre2: @socre2\n";
%socre = (
"kevin" => 100,
"xiang" => 50,
"jie" => 150,
"xxx" => 1,
"yyy" => 50
);
sub by_socre_value_and_key{
$socre{$a} <=> $socre{$b}
or
$b cmp $a;
}
@socre3 = sort by_socre_value_and_key keys %socre;
print "socre3: @socre3\n";