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";
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章