原创哈希数据导出算法
生活随笔
收集整理的這篇文章主要介紹了
原创哈希数据导出算法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
#Oracle客戶端配置 ORACLE_HOME=/export/home/tmn/oracle/product/11.2.0/dbhome NLS_LANG="SIMPLIFIED CHINESE_CHINA.ZHS16GBK" export ORACLE_HOME NLS_LANG PATH=$PATH:$ORACLE_HOME/bin export PATH LD_LIBRARY_PATH=/usr/lib64:/export/home/tmn/oracle/product/11.2.0/dbhome/lib32:/export/home/tmn/oracle/product/11.2.0/dbhome/lib:/usr/local/lib export LD_LIBRARY_PATH#對日期格式化處理 NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS' NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS' export NLS_DATE_FORMAT NLS_TIMESTAMP_FORMATalias echo='echo -e' SHELL=/bin/bash LC_ALL=zh_CN.GB18030 JAVA_JRE=/usr/jdk6/jre NLS_LANG=AMERICAN_AMERICA.ZHS16GBK LD_LIBRARY_PATH=/usr/lib64:/export/home/tmn/oracle/product/11.2.0/dbhome/lib32:/export/home/tmn/oracle/product/11.2.0/dbhome/lib:/usr/local/lib PATH=.:/export/home/tmn/oracle/product/11.2.0/dbhome/bin:.:/export/home/tmn/oracle/product/11.2.0/dbhome/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/jdk6/bin:/usr/jdk6/jre/bin:/sbin:/usr/bin:.:/export/home/tmn/bin:/usr/jdk6/bin:/usr/jdk6/jre/bin:/sbin:/usr/bin:. JAVA_HOME=/usr/jdk6 ODBCSYSINI=/usr/local/unixODBC/etc ODBCINI=/usr/local/unixODBC/etc/odbc.ini ODBCHOME=/usr/local/unixODBC CLASSPATH=/usr/jdk6/lib:/usr/jdk6/jre/lib:. ORACLE_HOME=/export/home/tmn/oracle/product/11.2.0/dbhome export SHELL NLS_LANG LC_ALL JAVA_JRE LD_LIBRARY_PATH PATH JAVA_HOME ODBCSYSINI export ODBCSYSINI ODBCINI ODBCHOME CLASSPATH ORACLE_HOME #!/bin/perl ################################################################################ ## 導出模板數據到excel ## 編寫:清風 ## 日期:2012/8/24 星期五 17:08:21 沈陽 ## 用法:perl main.pl 用戶名 密碼 server 生成屬性目錄 數據字典位置 生成excel目錄 #################################################################################傳入參數 my $username=shift; #用戶名 my $password=shift; #密碼 my $dbserver=shift; #server my $attr_dir=shift; #生成屬性目錄 my $dict_dir=shift; #數據字典位置 my $excel_dir=shift; #生成excel目錄 my $TEMPLATE_ID=shift; #模板ID my $user=shift; #用戶賬號 my $condition=shift; #查詢條件 my $mobile=shift; #手機號#系統參數 my $maxcpu=80; my $maxmem=5000000;#引用模塊 use DBI; use Shell; use Encode; use Encode qw(decode); use POSIX qw(strftime); #格式化時間 use Data::Dumper; use Getopt::Long; use DBD::Oracle; use Time::Local; use Time::HiRes; #為了得到毫秒的數據 use Spreadsheet::WriteExcel; use Excel::Writer::XLSX;my $cpu = getcpu(); my $mem = getmem(); print "user:$cpu\n"; print "user:$mem\n"; if($cpu<$maxcpu && $mem<$maxmem) {chomp($mobile);if($mobile ne ''){$context="綜合資源服務器負荷已經到達極限,請稍后再使用數據導出功能!";my $dbh_mss=DBI->connect('dbi:ODBC:sms','SGSHRUser','SGSHRPwd');my $dbh_ora=DBI->connect("DBI:Oracle:rmw","rmw2","rmw2!23lc");my $sth_mss=$dbh_mss->prepare("exec sp_Messages_Send '$mobile','$context',0,0,0,'ZHZY',0,'','',''");my $sth_ora=$dbh_ora->prepare("insert into app_sms (mobile, content, time_stamp, issend) values ('$mobile','$context',sysdate,'1')");$sth_mss->execute();$sth_ora->execute();$sth_mss->finish();$sth_ora->finish();$dbh_mss->disconnect();$dbh_ora->disconnect();}#釋放內存print `free && sync && echo 3 > /proc/sys/vm/drop_caches && echo "" && free`;exit 0; }#定義系統模板表和屬性表 $MY_TEMPLATEDEF="A_TEMPLATEDEF"; $MY_RESATTRIBUTE="M_RESATTRIBUTE"; #查詢條件 chomp($condition); if($condition ne '1=1') {$condition="and city_id=".$condition; } else {$condition="and ".$condition; }#print "獲取程序開始運行時間,以秒為單位計時..."."\n"; $START_TIME=time(); #print "連接數據庫..."."\n"; $dbh = DBI->connect("DBI:Oracle:$dbserver","$username","$password") || die ('connecting failed!'); $dbh->{LongTruncOk} = true; $dbh->{LongReadLen} = "10000"; #print "注意:連接的數據庫是:$dbserver,用戶名是:$username..."."\n";#獲得模型英文名 $sql_sta= "SELECT TPLNETYPE FROM A_TEMPLATEDEF T WHERE T.TPID = '$TEMPLATE_ID'"; $sql = $dbh->prepare($sql_sta); $sql->execute(); my $RESCLASSENNAME = ''; while( my @sql_column = $sql->fetchrow_array() ) {$RESCLASSENNAME=$sql_column[0];#表名 }if($RESCLASSENNAME ne '') {if($TEMPLATE_ID ne ''){$RESCLASSENNAME=uc($RESCLASSENNAME);#轉換成大寫$sql_sta= "SELECT DSTABLENAME FROM M_RESCLASS WHERE RESCLASSENNAME = '$RESCLASSENNAME'";$sql = $dbh->prepare($sql_sta); $sql->execute();my $dstablename = '';while( my @sql_column = $sql->fetchrow_array() ){$dstablename=$sql_column[0];#表名}my $sql_sta = "SELECT TPLCNNAME,TPLENNAME FROM ".$MY_TEMPLATEDEF." WHERE STATEFLAG=0 AND TPID =$TEMPLATE_ID";my $filename = "";$sql = $dbh->prepare($sql_sta); $sql->execute();while( my @sql_column = $sql->fetchrow_array() ){$filename=$sql_column[0];#模板中文名#$filename=$sql_column[1];#模板英文名}#導出M_RESATTRIBUTE表數據到文件中以^_^分割my $resattribute_ini=$attr_dir."/".$user."_".$RESCLASSENNAME."_resattribute.ini";my $sqlcontenttmp="SELECT RESCLASSENNAME,ATTRIBUTECOLUMNNAME,ATTRIBUTEENNAME,ATTRIBUTECNNAME,IFCHANGE,INPUTTYPE,SOURCEEXPRESSION,DATATYPE FROM ".$MY_RESATTRIBUTE." WHERE RESCLASSENNAME='$RESCLASSENNAME'";export_data($dbserver,$username,$password,$sqlcontenttmp,$resattribute_ini);#導出系統模版表數據到文件中以^_^分割my $templatedef_ini=$attr_dir."/".$user."_".$RESCLASSENNAME."_templatedef.ini";$sqlcontenttmp="SELECT A.TPLNETYPE, B.RESCLASSCNNAME, A.TPLCOLDESC FROM ".$MY_TEMPLATEDEF." A, M_RESCLASS B WHERE A.TPLNETYPE = B.RESCLASSENNAME AND A.TPID = '$TEMPLATE_ID'";export_data($dbserver,$username,$password,$sqlcontenttmp,$templatedef_ini);#導出字典關聯翻譯屬性到文件中以^_^分割#select int_id,zh_label from RMS_EQUIPROOM where stateflag=0 中 int_id,zh_label 不可以有空格open(RFILE1,"<$resattribute_ini") || die "1.Can not open $resattribute_ini : $!\n"; while(defined($line=<RFILE1>)){chomp($line);@field=split(/\^\_\^/,$line);$attributecolumnname=$field[1];$attributeid=$field[2];$inputtype=$field[5];$sourceexpression=$field[6];$datatype=$field[7];if($inputtype eq 'select' || $inputtype eq 'pageselectmore'){#獲得sourceexpression的表名@fieldes=split(/\s+/,$sourceexpression);@id_name=split(/,/,uc($fieldes[1]));$dict_table=$fieldes[3];#獲得sourceexpression的value名$id_name=$id_name[0];if($dict_table ne 'g_com_dict' & $datatype eq 'integer'){$sourceexpression=~s/$dict_table/$dict_table a/;$sourceexpression=$sourceexpression.' and exists(select 1 from '.$dstablename.' b where stateflag=0 and b.'.$attributecolumnname.' = a.'.$id_name.' )';}my $dict_file=$dict_dir."/".$user."_".$RESCLASSENNAME."_".$attributecolumnname.".ini"; #字典導出文件名$sqlcontenttmp=$sourceexpression;open(RFILE2,"<$templatedef_ini") || die "Can not open $templatedef_ini : $!\n"; while(defined($line=<RFILE2>)){# TPLNETYPE TPLCNNAME TPLCOLDESC #---------- --------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#JTWIND_CON 系統默認查詢 int_id,city_id,is_wrong,wrong_info,zh_label,related_equiproom,equ_type,equ_subclass,ne_code,asset_no,model,vendor,brand,supplier,company_maintain,start_time,end_time,status,fan_num,output_cap,maintain_person,flowid,modify_by,modify_date,stateflag,time_stamp@field=split(/\^\_\^/,$line);$tplcoldesc=$field[2];@col_list=split(/\,/,$tplcoldesc);for(my $i=0;$i<@col_list;$i++){my $col_name=trim($col_list[$i]);if($col_name eq $attributeid){export_data($dbserver,$username,$password,$sqlcontenttmp,$dict_file);}}}}}print "hash_join($dict_dir,$excel_dir,$resattribute_ini,$templatedef_ini,$filename,$dbserver,$username,$password,$dstablename,$mobile,$condition)\n";hash_join($dict_dir,$excel_dir,$resattribute_ini,$templatedef_ini,$filename,$dbserver,$username,$password,$dstablename,$mobile,$condition);#print "導出數據用時".reckon_runtime($START_TIME)."秒\n";} }#!/bin/perl ################################################################################ ## 生成導出模板數據的SQL語句,將數據導出到excel ## 編寫:清風 ## 日期:2012/8/24 星期五 18:09:23 沈陽 ## ################################################################################ sub hash_join {my $dict_dir=shift;my $excel_dir=shift;my $attr_file=shift;my $tmp_file=shift;my $filename=shift;my $dbserver=shift;my $username=shift;my $password=shift;my $dstablename=shift;my $mobile=shift;my $condition=shift;my %col_id_hash=(); #id對應的字段my %col_zhname_hash=(); #id對應的字段中文名my @need_decode_array=(); #存放需要翻譯的字段my %col_inputtype_hash=(); #INPUTTYPE哈希數據為0的不處理,為1的用函數idname()my $resclassenname;if (!$excel_dir || !$dstablename ||!$attr_file || !$tmp_file || !$dict_dir || !$dbserver|| !$username || !$password){die "Usage: perl 參數不正確!\n";}open(RFILE1,"<$attr_file") || die "2.Can not open $attr_file : $!\n"; while(defined($line=<RFILE1>)){#RESCLASSENNAME ATTRIBUTECOLUMNNAME ATTRIBUTEENNAME ATTRIBUTECNNAME IFCHANGE INPUTTYPE SOURCEEXPRESSION#-------------- ------------------- --------------- --------------- -------- --------- ----------------#IPACCESS related_server_id related_server_id AR(或BR/AR)承載業務局 1 pageselectmore select int_id,zh_label from V_RMS_IPACCESS where stateflag=0#IPACCESS capacity capacity 承載業務容量(單位:萬用戶) 0 input #IPACCESS con_mss_name con_mss_name 對接的CE設備名稱 2 select select a.value,a.name from g_com_dict a,g_com_dict b where a.super_code=b.code and b.dict_index='COMM_MATCH' order by a.name#IPACCESS con_mss_port con_mss_port 對接的CE設備端口 1 select select int_id,zh_label from RMS_GPRSPORT where stateflag=0chomp($line);@field=split(/\^\_\^/,$line);$resclassenname=$field[0];$attributecolumnname=$field[1];$resattributeid=$field[2];$attributecnname=$field[3];$ifchange=$field[4];$inputtype=$field[5];$sourceexpression=$field[6];#模型字段和物理字段對應關系$col_id_hash{$resattributeid}=${attributecolumnname};#物理字段和字段名稱對應關系$col_zhname_hash{$resattributeid}=${attributecnname};#INPUTTYPE輸入類型,默認為0,pageselectmore為1$col_inputtype_hash{$attributecolumnname}=0;#對于翻譯,字典翻譯有2種,其他的都一樣if($ifchange eq 0){next;#對于不需要翻譯的不做處理}#沒有字典表的字典翻譯if($ifchange eq 2 && $sourceexpression=~/\;/ ){$dict_file=$dict_dir."/".$attributecolumnname.".ini";%$attributecolumnname=();@dict_list=split(/\;/,$sourceexpression);foreach(@dict_list){if($_=~/\,/){@dict_data=split(/\,/,$_);${$attributecolumnname}{$dict_data[0]}=$dict_data[1];}else{${$attributecolumnname}{$_}=$_;}#print "$attributecolumnname ${$attributecolumnname} $dict_data[0] $dict_data[1]\n";}@need_decode_array=(@need_decode_array,$attributecolumnname);next;}#關聯翻譯,有字典表的翻譯同樣處理。if($ifchange ne 0 ){if($inputtype eq 'select'|| $inputtype eq 'pageselectmore'){$dict_file=$dict_dir."/".$user."_".$RESCLASSENNAME."_".$attributecolumnname.".ini";%$attributecolumnname=();if( -s $dict_file )#如果存在$dict_file則打開,如果不加判斷可能會報錯.{open(RDICT,"<$dict_file") || next "3.Can not open $dict_file : $!\n"; while(defined($dict_line=<RDICT>)){chomp($dict_line);@dict_data=split(/\^\_\^/,$dict_line);${$attributecolumnname}{$dict_data[0]}=$dict_data[1];}@need_decode_array=(@need_decode_array,$attributecolumnname);close(RDICT);}unlink ($dict_file);}}}close(RFILE1);open(RFILE2,"<$tmp_file") || die "4.Can not open $tmp_file : $!\n"; while(defined($line=<RFILE2>)){# TPLNETYPE TPLCNNAME TPLCOLDESC #---------- --------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#JTWIND_CON 系統默認查詢 int_id,city_id,is_wrong,wrong_info,zh_label,related_equiproom,equ_type,equ_subclass,ne_code,asset_no,model,vendor,brand,supplier,company_maintain,start_time,end_time,status,fan_num,output_cap,maintain_person,flowid,modify_by,modify_date,stateflag,time_stamp#MSSTRUNKGRP 系統默認查詢 tkgp_direction,zh_label,city_id,status,rate,type,a_equiproom_id,source_ne_id,source_port_id,z_equiproom_id,remote_ne_id,remote_port_id,isunidirectional,time_stamp,stateflag,remark,creator,creat_time,modifier,modify_time@field=split(/\^\_\^/,$line);$tplnetype=$field[0];$resclasscnname=$field[1]; $tplcoldesc=$field[2];@col_list=split(/\,/,$tplcoldesc);@excel_tile=();@excel_col=();$unload_sql="select "; for(my $i=0;$i<@col_list;$i++){my $value=trim($col_list[$i]);$col_name=$col_id_hash{$value};$col_zhname=$col_zhname_hash{$value};@excel_tile=(@excel_tile,$col_zhname);@excel_col=(@excel_col,$col_name);#回車換行導致導出文件不對,串行修改#$unload_sql=$unload_sql."replace(replace(".$col_name.",chr(13),''),chr(10),''),"$unload_sql=$unload_sql.$col_name.","}chop($unload_sql);$unload_sql=$unload_sql." from ".$dstablename." where stateflag=0";}$unload_sql=$unload_sql.' '.$condition;print "\n導出SQL語句:\n$unload_sql\n\n";close(RFILE2);#導出數據文件名$file_name=$excel_dir.'/'.$user.'_'.$dstablename.'.ini';#導出模版數據export_data($dbserver,$username,$password,$unload_sql,$file_name);#print "\n";#生成excel文件名my $timeStr = strftime "%Y-%m-%d.%H.%M", localtime;#$excel_file=$excel_dir.'/'.$user.'_'.$resclassenname.'.'.$timeStr.'.xlsx';#my $url=$user.'_'.$resclassenname.'.'.$timeStr.'.xlsx';$excel_file=$excel_dir.'/'.$user.'_'.$resclasscnname.'.'.$timeStr.'.xlsx';my $url=$user.'_'.$resclasscnname.'.'.$timeStr.'.xlsx';print "$url\n";#如果導出數據文件不為空,則創建excel文件if( -s $file_name ){my $inputDataFile=$file_name;my $outputDataFile=$excel_file;my @report_title=@excel_tile;my @table_cols=@excel_col;my @decode_cols=@need_decode_array;my $max_row=900000; #設定生成EXCEL2007最大行數(90萬),實際可以支持1048576 my $max_col=16384; #設定生成EXCEL2007最大列數(400),實際可以支持16384my $all_row=0;my $row=0;my $sheet_num=1;my $workBook = Excel::Writer::XLSX->new($outputDataFile);#excel表頭樣式my $format_header = $workBook->add_format(bold =>1, border => 1);$format_header->set_font(decode('gbk','Tahoma'));$format_header->set_size( 11 );#excel body樣式my $format_body = $workBook->add_format(border => 1);$format_body->set_font('Tahoma');$format_body->set_size( 11 );#添加模板數據sheet$sheet_name=$workBook->add_worksheet(decode('gbk',$filename));#excel表頭my $colNum=@report_title;for my $col (0..$colNum-1){$value=trim($report_title[$col]);$sheet_name->write($row,$col,decode('gbk',$value), $format_header);}$row+=1;#excel bodyopen (SOURCEDATAFILE, "<$inputDataFile");while(defined($line=<SOURCEDATAFILE>)){chomp($line);my @cols=split(/\^\_\^/,$line);%col_ps=remenber_positon_by_id(@table_cols);foreach(@decode_cols){my $word=$_;$data_ps=$col_ps{$_}; #得到字段在數組中的位置$decode_befor=$cols[$data_ps]; #得到相應字段的值if($decode_befor =~ /^(.*\,.*)$/){my @cols_sub=split(/\,/,$decode_befor);for $n (0..$#cols_sub){$decode_befor_sub=$cols_sub[$n];$new_data_sub=${$word}{$decode_befor_sub};$cols_sub[$n]=$new_data_sub;}$cols[$data_ps]=join ',',@cols_sub;}else{if($data_ps ne ''){$new_data=${$_}{$decode_befor};if($new_data ne ''){$cols[$data_ps]=$new_data; #賦給新的值}}}}if($row<$max_row){ my $col=0;while($col<$colNum){if($col<$max_col){$value=trim($cols[$col]);$sheet_name->write($row, $col, decode('gbk',$value));#$sheet_name->write($row, $col, decode('gb2312',$value), $format_body);$col++;}else{#print "注意:超出設定最大列數:$max_col,后續字段不再導出!\n";#print "注意:請調整程序配置最大列數!\n";exit;}}}else{$sheet_num++;$row=0;#添加模板數據sheet$sheet_name=$workBook->add_worksheet(decode('gbk',$filename.$sheet_num));#excel表頭$colNum=@report_title;for my $col (0..$colNum-1){$value=trim($report_title[$col]);$sheet_name->write($row,$col,decode('gbk',$value), $format_header);}$row++;my $col=0;while($col<$colNum){if($col<$max_col){$value=trim($cols[$col]);$sheet_name->write($row, $col, decode('gbk',$value));#$sheet_name->write($row, $col, decode('gb2312',$value), $format_body);$col++;}else{#print "注意:超出設定最大列數:$max_col,后續字段不再導出!\n";#print "注意:請調整程序配置最大列數!\n";exit;}}}$row++;$all_row++;if($row%10000==0){my $cpu = getcpu();my $mem = getmem();print "$cpu\n";print "$mem\n";print "$row\n";if($cpu<$maxcpu && $mem<$maxmem){chomp($mobile);if($mobile ne ''){$context="綜合資源服務器負荷已經到達極限,請稍后再使用數據導出功能!";my $dbh_mss=DBI->connect('dbi:ODBC:sms','SGSHRUser','SGSHRPwd');my $dbh_ora=DBI->connect("DBI:Oracle:rmw","rmw2","rmw2!23lc");my $sth_mss=$dbh_mss->prepare("exec sp_Messages_Send '$mobile','$context',0,0,0,'ZHZY',0,'','',''");my $sth_ora=$dbh_ora->prepare("insert into app_sms (mobile, content, time_stamp, issend) values ('$mobile','$context',sysdate,'1')");$sth_mss->execute();$sth_ora->execute();$sth_mss->finish();$sth_ora->finish();$dbh_mss->disconnect();$dbh_ora->disconnect();}#釋放內存print `free && sync && echo 3 > /proc/sys/vm/drop_caches && echo "" && free`;exit 0;}}}$row-=1;$all_row-=1;$workBook->close();if ($row < 1){unlink ($outputDataFile);}unlink ($file_name);chomp($mobile);if($mobile ne ''){$context="數據導出:文件[".$filename."]共導出".$all_row."行,耗時".reckon_runtime($START_TIME)."秒,請到[基礎功能-數據導出]模塊下載!";my $dbh_mss=DBI->connect('dbi:ODBC:sms','SGSHRUser','SGSHRPwd');my $dbh_ora=DBI->connect("DBI:Oracle:rmw","rmw2","rmw2!23lc");my $sth_mss=$dbh_mss->prepare("exec sp_Messages_Send '$mobile','$context',0,0,0,'ZHZY',0,'','',''");my $sth_ora=$dbh_ora->prepare("insert into app_sms (mobile, content, time_stamp, issend) values ('$mobile','$context',sysdate,'1')");$sth_mss->execute();$sth_ora->execute();$sth_mss->finish();$sth_ora->finish();$dbh_mss->disconnect();$dbh_ora->disconnect();}}else{#print "查詢數據庫為空,刪除文件$file_name...\n";unlink ($file_name);} }################################################################################ ## 導出數據庫數據到文件 ## 編寫:清風 ## 日期:2012/8/23 星期四 13:26:51 沈陽 ## ################################################################################ sub export_data {my $dbserver=shift; #數據庫信息my $username=shift;my $password=shift;my $sql=shift; #sql語句my $file_name=shift;#執行sql后生成的文件名my @field; #sql語句執行結果數組#print "連接數據庫...\n";my $dbh = DBI->connect("DBI:Oracle:$dbserver","$username","$password");if($dbh){#print "連接數據庫成功...\n";}else{#print "連接數據庫失敗...\n";exit(1);}my $filename=`basename $file_name`;#print "$file_name\n";open(WFILE,">$file_name") || die "打開文件失敗!\n";#執行SELECT語句并獲取數據my $sth = $dbh->prepare("$sql");$sth->execute();#讀取sql語句執行結果數據while(@field=$sth->fetchrow_array){foreach(@field){$_=~s/[\r\n]+/ /g;#替換換行回車}write_file(WFILE,(@field));print WFILE "\n";}close(WFILE);$sth->finish();#print "關閉數據庫...\n";$dbh->disconnect(); }#################################################################################### ## 函數名稱:write_file ## 函數功能:將輸入參數指定的數組寫入文件中 ## 輸入參數:$WRFILE 要寫入數據的文件句柄 ## @row_data 要寫入文件中一行數據 ## 輸出參數:無 ## 返回 值: 無 ## 說 明: #################################################################################### sub write_file {my $i=0;my ($W_FILE,@row_data)=@_;my $col_num=@row_data;for($i=0;$i<$col_num;$i++){$row_data[$i]=trim($row_data[$i]);#$mid_data=encode("gb2312",decode("gbk",$row_data[$i]));print $W_FILE $row_data[$i].chr(94).chr(95).chr(94);} }#################################################################################### # 函數名稱:remenber_positon_by_id # 函數功能:記住標題的位置,防止標題順序改變和使數據異常 # 輸入參數:@title 要分析的標題 # # 輸出參數:%positonlist 以標題為索引的數組 #################################################################################### sub remenber_positon_by_id {local(@title)=@_;my $title_num = @title;my %positonlist=();for($i=0; $i<$title_num; $i++){$mid_titel=$title[$i];$positonlist{$mid_titel}=$i;}return (%positonlist); }#################################################################################### ## 函數名稱:trim ## 函數功能:將輸入變量中的前后空格去掉。輸入變量的值不變,返回去空格后的值 ## 輸入參數:$line 要去除前后空格的字符串變量 ## 輸出參數:無 ## 返回 值: 去掉前后空格后的字符串變量 ## 說 明: #################################################################################### sub trim {my ($line)=@_;$line=~s/^[ \t]+//; #替換掉開頭空格$line=~s/[ \t\r\n]+$//;#替換掉結尾空格return $line; } #################################################################################### ## 計算入參到調用該Perl程序時經過的時長,以S為單位 ## 日期:2011.03.03 北京 ## 功能: 計算入參到調用該Perl程序時經過的時長,以S為單位 ## 腳本說明: ## 入參1:起始時間 ## 要求參數格式如下:2011-03-03 10:10:10 #################################################################################### sub reckon_runtime() {$start_time=shift;#起始時間$cur_time=time();#當前時間$result=0;$result_sec=$cur_time-$start_time;if($start_time ne ''){#如果時間超過1分鐘,顯示分鐘if($result_sec<=60){$result=$result_sec;}elsif($result_sec>60){$result_min = int($result_sec/60);$result_sec = $result_sec%60;$result = $result_min."分".$result_sec;}else{$result=$result_sec;}#如果時間超過60分鐘,顯示時鐘#如果時間超過24小時,顯示天數}else{$result="null";} } #################################################################################### ## 獲取CPU、內存 ## 編寫:清風 ## 日期:2015/12/29 13:03:54 沈陽 #################################################################################### sub getcpu {@cpu=split(/\n/,`top -b -n 2|grep %id`);@cpu=split(/,/,$cpu[1]);@cpu=split(/\%/,$cpu[3]);$cpu=trim($cpu[0]);return $cpu; }sub getmem {@mem=split(/,/,`top -b -n 1|grep Mem`);@mem=split(/k/,$mem[2]);$mem=trim($mem[0]);return $mem; } ;?
轉載于:https://my.oschina.net/fengyunfu/blog/738927
總結
以上是生活随笔為你收集整理的原创哈希数据导出算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用锁实现多线程共用一个数据源
- 下一篇: php的filesystem基本函数的学