#!/usr/bin/perl -w
#########################################################################
# File Name: test.pl
#########################################################################
print "hello word \n";
print "---- 变量定义用\$ 符号------\n";
$a=`echo xxxx`;
print "$a";
$b='aaaa'.'hhhh';
$c="bbbb";
$d="$b"."$c";
$d.='eeee';
print "$d\n";
print "d: $d\n";
$bl1 = 1>=0;
$bl2 = 1>=3;
print $bl1;
print $bl2;
print "---- if elsif else 条件判断语句 ----\n";
$if1=2;
if($if1 == 1){
print "pass\n";
}elsif($if1 == 2){
print "==2\n";
}else{
print "fail\n";
}
#chomp($in1 = <STDIN>);
#print "your input: $in1\n";
#if(defined($in2)){
# print "1";
#}else{
# print "2";
#}
print " ------- \@ 符号来定义数组 ------ \n";
@array = (1,2,3,"4")x4;
print "$array[1]\n";
print "$#array\n";
print "$array[-1] \n";
$xtest="123"x5;
print "$xtest\n";
print "pop push 针对数组的末尾操作 \n";
print "pop 取出最后一个,返回数组剩下的, push将一个元素添加到末尾 \n";
print "shift unshift 为 pop push的反向操作,针对数组头\n";
@array2 = 5..10;
print "$#array2 ---- $array2[-1] \n";
pop @array2;
print "$#array2 ---- $array2[-1] \n";
pop(@array2);
print "$#array2 ---- $array2[-1] \n";
print "---------- array3 ----------\n";
@array3 = 1..3;
print "$#array3 ---- $array3[-1] \n";
push @array3,9;
print "$#array3 ---- $array3[-1] \n";
push @array3,10...12;
print "$#array3 ---- $array3[-1] \n";
print "---------- array4 ----------\n";
@array4 = 1..3;
print "@array4 ---- $array4[0] \n";
shift @array4;
print "@array4 ---- $array4[0] \n";
unshift @array4,99;
print "@array4 ---- $array4[0] \n";
unshift(@array4,'98');
print "@array4 ---- $array4[0] \n";
print "---------- array5 ----------\n";
@array5 = qw(eating rocks is wrong);
$array5 = "right";
print "@array5 ... $array5 \n";
print "this is $array5[3] \n";
print "this is ${array5}[3]\n";
print "this is $array5"."[3]\n";
print "this is $array5\[3] \n";
print "--------- for --------------\n";
for($i=0; $i<10; $i++){
print "for: $i\n";
}
#实际for和foreach是一样的
#中断循环对比C 的break和continue,Perl分别对应的是 last 和 next
#Perl中还特有 redo, 调到当前循环的顶端,不进行条件判断就再次执行本次循环
#next会进入下一次循环,redo会继续执行本次循环
print "--------- foreach ----------\n";
@rocks = qw(bedrock slate lava);
print "before rocks: \n @rocks \n";
foreach $rock (@rocks){
#如果有 redo 应该跳到的位置
$rock = "\t$rock";
$rock .= "\n";
print "new rock: $rock \n";
}
print "after rocks: \n @rocks \n";
print "--------- reverse 翻转 ----------\n";
@reverse1 = reverse 6..9;
print "@reverse1 \n";
@reverse2 = reverse @reverse1;
print "@reverse2\n";
print "--------- sort 排序 -------------\n";
@sort1 = qw(aaa ccc eee bbb ddd);
print "@sort1\n";
@sort1 = sort(@sort1);
print "@sort1\n";
print "--------- context -----------\n";
print "变量和列表会根据上下文给出不同的值\n";
print 1+@sort1."\n";
@rocks = qw(xxx aaa ccc);
print "I have",@rocks,"rocks!\n";
#强制转换为标量
print "I have", scalar @rocks, "rocks!\n";
print "--------- sub ----------------\n";
sub subtest1{
$temp=$a.$b;
$temp;
}
$a = "1";
$b = "2zzzzz";
$c = &subtest1;
print "c: $c\n";
sub addfunc{
$size = @_;
print "param size: $size\n";
my($num1, $num2);
($num1, $num2) = @_;
$num1 + $num2;
}
$addresult = &addfunc(100,99);
print "addresult: $addresult\n";
print "-------- 子程序 == 函数 ----\n";
print "函数参数放在 \@_数组内,可以用shift循环的切出第一个参数\n";
print "my() 定义局部变量\n";
print "子程序的返回值就是最后一部的计算值\n";
sub max{
my($maxValue) = shift @_;
foreach(@_){
if($_>$maxValue){
$maxValue = $_;
}
}
$maxValue;
}
@numset = (100,600,3000,3001);
$maxValue = &max(@numset);
print "maxValue: $maxValue\n";
sub which_element_is{
if(@_ < 2){
return "输入参数个数不能小于2";
}
my($what, @array) = @_;
my $index = 0;
foreach(@array){
if($what eq $_){
#判断字符串相等不能使用==,只能用eq
print "Find this element [$_]: $index\n";
return $index;
}
$index++;
}
return "can't find this element";
}
@names = qw(kevin xiang jie);
$which_element = &which_element_is("jie", @names);
print "which_element: $which_element\n";
print "上面的程序都是不严谨语法, 开头添加 use strict 变为严谨语法检测\n";
print '定义变量时需要用my定义, my $baa = 1; '."\n";
Perl基本语法实例,变量,数组,函数,if, for, while
原创
©著作权归作者所有:来自51CTO博客作者nxf198609的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
量化交易开发之基本语法(三)
本教程则是以量化的情景从零讲解python编程,所以将更适合想学做量化策略的人。
数据 变量名 python -
Python中的if、while、for 语法及实例
Python中的if、while、for 语法及实例
Python while for