Perl基本语法实例,变量,数组,函数,if, for, while
2018-01-20 13:17
645 查看
#!/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";
#########################################################################
# 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
- Kotlin学习(二)—— 基本语法,函数,变量,字符串模板,条件表达式,null,类型检测,for,while,when,区间,集合
- JavaScript基础(基本语法:变量、语句、数组、函数、对象、全局方法以及js与java不同,js与html关联)
- shell的一些常用的语句(if语句,变量,for、while、until语句、函数调用、脚本调用)
- Python3学习(一)-基础、数据类型、变量、字符串和编码、list&tuple、if、for、while、dict、set、函数与参数
- android Monkeyrunner:Python语法知识学习(for,while,if用法实例)
- Python中的if、while、for 语法及实例
- Python基础语法(2)——字典、函数定义使用、控制流(if&for&while)
- Python_3_学习之路_(1)变量_用户交互_if_else___while__for
- [转]linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- Perl专题之函数(一)函数基本语法
- linux shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select 学习笔记
- if/while/for 、解决数学难题 、 Python实例
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- Python入门 列表 for while if...else... 函数
- 【03】for和while循环关键字,函数基本概念