Friday 15 January 2010

Printing array in Perl -


I currently read fstab files, split perl scripts according to columns, and search for the word in each column I am For the longest time to display this all the pecic (I think) works, the problem I have is that it keeps printing the same length for every line which is not true. Example $ dev_parts prints 24, and $ labe_parts print 24 and so on ...

Below is my code.

  #! Use / usr / bin / perl strict; Print "file name: \ n"; My $ file_name = & lt; STDIN & gt; Open (IN, "$ file_name"); My @parts = split (/ \ s + /, $ file_name); Prefer my $ usr_file (& lt; IN & gt;) {chomp ($ usr_file); @ Equal = partition (/ \ s + /, $ usr_file); Push (@dev, $ parts [0]); Push (@label, $ parts [1]); Push (@tmpfs, $ parts [2]); Push (@makes, $ 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 "is the longest word in the first line: $ dev_length2 \ n"; Foreign currency $ label_parse (@ labell) {$ label_text1 = length ($ parts [$ label_paras]); If ($ lab_lamp1> label $ lang 2) {$ label_length2 = $ Label_LANG1; }} Print "is the longest word in the first line: $ label_length2 \ n";    

Your code should be like this

  #! Use / usr / bin / perl strict; Use warnings; Use data: Dumper; Print "file name: \ n"; My $ file_name = & lt; STDIN & gt; Chomp ($ file_name); Open (FILE, "$ file_name") or $ d! My% colhash; While (& lt; FILE & gt;) {my $ col = 0; My @parts = split / \ s + /; Map {my $ lane = length ($ _); $ Col ++; If ($ colhash {$ col}} & lt; $ len) {$ colhash {$ col} = $ len; # Store the longest word length for each column}} @ Actually; } Print dumper (\% collage);    

No comments:

Post a Comment