#-------------------------------
#name:array_assign.pl
#assign a array and print the elements in it
#!/usr/bin/perl
@fruit=('banana','orange','apple','pear');
($a[1],$a[3],$a[5],$a[7])=@fruit ;
print "array a=@a/n";
#num=0
#`let num=$#a+1`
print "there are/t", $#a+1, "/telements in array a/n";
#!/usr/bin/perl
#chomp.cl
use strict;
use warnings;
while(my $a=<STDIN>)
{
#delete /n in every line
chomp $a;
print "your input is '$a'/n";
#exit this loop when your input is NULL
last if ($a eq '');
}
#------------------------------------------------------
#name:chomp.pl
#note:chomp used remove /n
#!/usr/bin/perl
print "input something please!!!";
$a=<STDIN>;
print "your input is $a";
print "input again";
chomp($b=<STDIN>);
print "your input is $b";
#----------------------------------------------
#name:choose.pl
#note:(?:) choose symbol inherit from C language
#!/usr/bin/perl
print "input your age.../n";
$age=<STDIN>;
#$msg=($age<21?"young":"old");
#print "you are $msg/n";
print $age<21?"you are young/n":"you are old/n";
#-------------------------------
#name:data.pl
#note:useful of __DATA__
#!/usr/bin/perl
while(<DATA>)
{
print $_ unless /app/;
}
__DATA__
apple;
orange;
grape;
#--------------------------------------------------
#name:delete.pl
#note:delete function could remove a element from hash
#!/usr/bin/perl
%employee=("driver"=>"mike",
"doorman"=>"tom",
"manager"=>"geogre");
print "old employees/n ";
while (($position,$name)=each(%employee))
{
print " $position:$name/n";
}
delete $employee{"doorman"};
print "new employees/n ";
while (($position,$name)=each(%employee))
{
print " $position:$name/n";
}
#-----------------------------------------
#name:do_while.pl
#note:useful of do while loop sentence
#!/usr/bin/perl
$a=0;
$b=0;
do
{
$a+=1;
print "$a";
}while($a<10);
print "/n";
do
{
$b+=1;
print "$b";
}until($b>=10);
print "/n";
#---------------------------------------
#name:each.pl
#note:each function return two elements
# random in hash
#!/usr/bin/perl
%place=(
"wh"=>"wuhan",
"dn"=>"daonian",
"qd"=>"qindao"
);
while(($a,$b)=each(%place))
{
print "$a=$b/n";
}
#------------------------
#name:eof.pl
#note:output statement block,advoid use multiple print
print<<EOF
hi all
I am Tao
EOF
#------------------------------------------------
#name:e_re.pl
#note:e means calculate in regular expressions
#!/usr/bin/perl
$_=2;
s/2/2+3/e;
print "the result is $_ /n";
#-------------------------------------------
#name:exists.pl
#note:return ture if the condition of exists is ture
#!/usr/bin/perl
@fruit=("apple","grape","pear","banana");
print "the first element is $fruit[0]/n",if exists($fruit[0]);
print "out of range/n",if not exists($fruit[5]);
#---------------------------------------------------
#name:foreach.pl
#note:useful of foreach loop function
#!/usr/bin/perl
foreach $hour (1..24)
{
if($hour>=6 && $hour<=12)
{
print "hour is $hour Good morning!!!/n";
}
elsif($hour>12 && $hour<18)
{
print "hour is $hour Good afternoon!!!/n";
}
print "hour is $hour Good evening!!!/n";
}
#-----------------------------------------
#name:getc.pl
#note:getc can get the first character your input,<> could
# store the other
#!/usr/bin/perl
print "put something/n";
$a=getc;
$b=<>;
print "the first character
of your input is $a/n";
print "the rest are $b/n";
#--------------------------------------------
#name:hash.pl
#note:assigning keys and values to a hash
%fruit=(ba=>"banana",ap=>"apple",
pe=>"pear",gr=>"grape");
print "ba=$fruit{ba}/n ap=$fruit{ap}/n"
#----------------------------------------------
#name:if.pl
#note:if function in perl
#!/usr/bin/perl
$hour=(localtime)[2];
if($hour>=6 && $hour<=12)
{
print "Good morning!!!/n";
}
elsif($hour>12 && $hour<18)
{
print "Good afternoon!!!/n";
}
else
{
print "Good evening!!!/n";
}
#-------------------------------------------------------
#name:join.pl
#note:join function could put the element in list together
# with special express
#!/usr/bin/perl
$a="pear";
$b="banaba";
$c="grape";
print join("/n",sort($a,$b,$c)),"/n";
#---------------------------------------------------
#name:next_last.pl
#note:next equal continue,and last equal break
#!/usr/bin/perl
while(1)
{
print "input your grade!/n";
$grade=<STDIN>;
if ($grade<0 || $grade>100)
{
print "illegal input!/n";
next;
}
elsif($grade>89 && $grade<101)
{print "you got A/n";}
elsif($grade>79 && $grade<90)
{print "you got B/n";}
elsif($grade>59 && $grade<80)
{print "you got C/n";}
else
{print "you are failed/n";}
print "are you want to input again? (y or n)/n";
chomp($answer=<STDIN>);
# next if $answer eq "y";
if ($answer eq "y")
{next;}
last if $answer eq "n";
}
#------------------------------------
#name:nested_hash.pl
#note:look care the usage of nested_hash
#!/usr/bin/perl
%student=(math=>{mike=>70,geogre=>90},
science=>{mike=>80,geogre=>70});
print "mike's math=$student{math}->{mike}/n
geogre's science=$student{science}->{geogre}/n";
#-----------------------------------------------------------
#name:pop_push_shift.pl
#note:pop and push process the last element in the list,
# shift process the first element
#!/usr/bin/perl
@names=("mike","hill","jimmy","tom");
print "the array names are:/t @names /n";
pop(@names);
print "after pop() ,the array names are:/t @names /n";
push(@names,"Geogre");
print "after push Geogre,the array names are:/t @names /n";
shift(@names);
print "after shift,the array names are:/t @names /n";
#----------------------------
#name:print.pl
#note:print current line and filename
#!/usr/bin/perl
print "line number:",__LINE__,"/n";
print "file name:",__FILE__,"/n";
__END__ #statement will be comment after this
hi all
#---------------------------
#name:read.pl
#note:read function in perl
#!/usr/bin/perl
print "input something please!!!,less than 10 bits/n";
$number=read(STDIN,$a,10);
print "your input is $a/n";
print "the number of bytes read was $number /n";
#-------------------------------------------------------
#name:redo.pl
#note:the loop will go on if you do not type yes
#!/usr/bin/perl
LABEL:
{
print "Are your hungry???/n";
chomp($answer=<STDIN>);
redo LABEL unless ($answer eq "yes");
}
print "have yourself please!!!/n";
#------------------------------------------
#name:split.pl
#note:split function could tranlate a string
# into array
#!/usr/bin/perl
$a="a:b:c:d:e:f";
@b=split(":",$a);
print "the array b :@b/n";
#------------------------------------------
#name:stdin.pl
#note:using <stdin>
#!/usr/bin/perl
print "input please/n";
#be careful of < >
$str=<stdin>;
print "your string is $str/n ";
#--------------------------------------
#name:swap_re.pl
#note:swap apple with pear ,and it ignore case sensitive
#!/usr/bin/perl
while(<DATA>)
{
s/apple/pear/ig ;
print ;
}
__DATA__
apple apple apple;
APPLE;
grape;
orange;
#!/usr/bin/perl
@a=(1..10);
#$b=$#a;
print " a=$#a/n";
#-------------------------------------
#name:until.pl
#note:pay attention to the effect of chomp function
#!/usr/bin/per
print "Are you OK ???/n";
chomp($answer=<STDIN>);
until($answer eq "yes")
{
print "Waiting you hit OK !!!/n";
chomp($answer=<STDIN>);
}
print "thank you/n";
#----------------------------------------------
#name:var_assignment.pl
#note:scalar,array,hash assignment,and print them
$scalar="hi all";
@array=('0','1','2','3','4','5');
%hash=(wh=>'wuhan',
bj=>'beijing',
qd=>'qindao',
);
print "scalar=$scalar/n";
print "array[1]=$array[1]/n";
print "all array=@array/n";
print "hash{wh}=$hash{wh}/n";