I currently have my Perl script to read fstab files, split them up by column and search for which word in each column is the longest to display it. All that works peachy (I think), the problem I'm having is that it keeps printing out the same length for every line which is not true. Example $dev_parts prints 24, and $labe_parts prints 24 and so on...
below is my code.
#!/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";
print arraystatements?$dev_partsor$label_parts. Also, those are not lengths, they are used as array indexes. Also, they are used as array indexes in the same array@parts. So why would they not print the same values? Also, you do not assign anything to$dev_length2or$label_length2, which means that withoutwarningsturned on they are silently converted to 0.use warningsand fix the warnings that appear. Adduse Data::Dumperand addprint Dumper \@partsetc statements for variables to see what they contain.