Perl 程序 計算各個 字段的長度

具體請點擊:http://www.verydemo.com/demo_c152_i49050.html

今天開始搭建MySQL數據庫了,現在手裏已經有每條記錄了,字段間以'\t'分隔開,建表的時候需要確定個字段的長度,於是,寫了下面的腳本,打印各字段的最大長度
源代碼(maxlength.pl)如下:
1 #!/usr/bin/perl
2 my $in = $ARGV[0];
3 die "no input file" if(!defined($in));
4 open(RH,$in) or die "Cannot read $in:$!";
5 my @lines = <RH>; chomp @lines;
6 close RH or die "Cannot close $in:$!";
7 my @maxlengths;
8 for(my $i = 0; $i < @lines; $i++){
9 my @fields = split(/\t/,$lines[$i]);
10 for(my $j = 0; $j < @fields;$j ++){
11 if(length($fields[$j]) > $maxlengths[$j]){
12 $maxlengths[$j] = length($fields[$j]);
13 }
14 }
15 }
16 print join('--',@maxlengths),"\n";
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章