1、die if以及文件的操做數組
#!/usr/bin/perl -w use strict; die "USAGE:\n\tperl $0 <><><>\n\n" if @ARGV != 3; #$0表示腳本名,@ARGV表示參數個數 open (INPUT1 ,"$ARGV[0]") or die "can not open $ARGV[0]!"; open (QUERY ,"$ARGV[1]") or die "can not open $ARGV[1] !"; open (OUT ,">$ARGV[2]") or die "can not open $ARGV[2]!"; #注意下標是從0開始,>表示以寫入方式打開 my %h; while(<INPUT1>) { chomp; my @line = split"\t"; $line[0] =~ s/\s//g; $line[2] =~ s/\s//g; my $k = $line[0]."\t".$line[2]; $h{$k}=$k; } my $header =readline(QUERY);#讀取一行 print OUT $header;#寫入用print my %header_idx; chop($header); my @headers = split("\t", $header);#對變量進行split #scalar()獲取數組長度 for(my $i=0; $i < scalar(@headers); $i++){ $headers[$i]=~s/\s//g; $header_idx{$headers[$i]}=$i; }
2、遍歷哈希函數
while ( my ($key,$value) = each %ENV ) { print "$key:$value\n"; }
3、時間打印spa
my $datestring = localtime(); print "starting time: $datestring \n"; #Fri Oct 19 15:36:21 2018這樣的英文結果也許並不理想 sub sub_format_datetime { my($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = @_; $wday = $yday = $isdst = 0; sprintf("%4d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $day, $hour, $min, $sec); } #sprintf()函數 ''' $result = sprintf("%08d",$number);讓$number有8個前導零。 $rounded = sprintf("%.3f",$number); 讓小數點後有3位數字 ''' my $start = sub_format_datetime( localtime() ); print $start; print "\n"; #2018-10-19 15:36:21變成中文時間