我目前有我的 Perl 脚本来读取 fstab 文件,按列拆分它们并搜索每列中的哪个单词最长以显示它。所有这些都很有效(我认为),我遇到的问题是它会为每一行打印出相同的长度,这是不正确的。示例 $dev_parts 打印 24,$labe_parts 打印 24,依此类推...
下面是我的代码。
#!/usr/bin/perl
use strict;
print "Enter file name: \n";
my $file_name = <STDIN>;
open(IN, "$file_name");
my @parts = split( /\s+/, $file_name);
foreach my $usr_file (<IN>) {
chomp($usr_file);
@parts = split( /\s+/, $usr_file);
push(@dev, $parts[0]);
push(@label, $parts[1]);
push(@tmpfs, $parts[2]);
push(@devpts, $parts[3]);
push(@sysfs, $parts[4]);
push(@proc, $parts[5]);
}
foreach $dev_parts (@dev) {
$dev_length1 = length ($parts[$dev_parts]);
if ( $dev_length1 > $dev_length2) {
$dev_length2 = $dev_length1;
}
}
print "The longest word in the first line is: $dev_length2 \n";
foreach $label_parts (@label) {
$label_length1 = length($parts[$label_parts]);
if ($label_length1 > $label_length2) {
$label_length2 = $label_length1;
}
}
print "The longest word in the first line is: $label_length2 \n";