How can I correctly calculate the lengths of fields in a CSV dcoument using Perl? -


I have a datas and a simple while is to perform an operation with a Perl script here dataset Is a small withdrawal from:

"number", "code", "country", "gamma", "x1", "x2", "x3", "x4", " X5 "," X6 "1," DZA "," Algeria "," 0.01 ", 7.44,47.3,0.46,0,0,0.13 2," Agu "," Angola "," 0.00 ", 6.7 9," NULL ", 0.21,1,0,0.28 3," Ben "," Benin "," - 0.01 ", 7.02,38.9,0.27,1,0,0,05 4," BWA "," Botswana "," 0.06 " , 6.28,45.7,0.42,1,0,0.07 5, "HVO", "Burqina Faso", "0.00", 6.15,36.3,0.08,1,0,0,05 6, "BDI", "Burundi", "0.00", 6.38,41.8,0.18,1,0,0

The script should calculate different fields from each , , and store the highest values ​​in an array.

However, savings do not work properly. Here's a part of the code:

  @maxl = Map length, @ter ms; While (`<{INFILE & gt;`} {$ _ = ~ s / [\ "\ n] // g; @terms = split / $ sep /, $ _; @lengths = map length, @ter ms; ($ K = 0, $ k & lt; = $ # terms, $ k ++) {if ($ length [$ k]> $ maxl [$ k]) {$ maxl [$ k] = The $ lenghts [$ k];}} print uses "partitions" before the "@landies \ n";}  

code where this dataset I use a print command to see the values ​​of the operation:

1 3 7 4 4 4 1 1 5

While in loop I see other values I use another print statement for, I get:

  1 3 4 6 4 4 4 1 1 4 1 3 5 4 4 1 4 4 1 1 4 1 3 8 4 4 4 4 1 1 4 1 3 12 4 4 4 4 1 1 4 1 3 7 4 4 4 1 1 1 1 3 4 4 4 4 1 1 4 1 3 4 4 4 4 4 4 1 1 4 1 3 16 5 4 4 4 1 1 4 2 3 4 5 3 4 4 1 1 4 2 3 7 4 4 4 1 1 4 2 3 4 4 4 4 1 4 4 4 4 4 4 1 4 4 4 4 4 4 4 4 1 4 4 4 1 1 4 4 3 4 4 4 1 1 1 4  

Fourth column e.g. Obviously the value is greater than 3. While the loop should save the highest values ​​and move those values ​​to @maxl .

What was wrong?


for commas in the loop are incorrect

 for  ($ k = 0, $ k   

There is a typo here for the beginning $ maxl [$ k] = $ lenghts [$ k]; (which is 'strict use')

from comma Consider using it for more reliable parsing of separated data (this can also handle other separators):

  #! / Usr / bin / perl string; warning Use: Text: Use CSV; my $ csv = Text :: CSV- & gt; New (); My @ Max_Lambels; while (my $ line = & lt; INFILE & gt;) {die Unable to parse "$ line '' 'as long as $ csv-> parse ($ line); My @column_lengths = map {length} $ csv-> Field (); My $ i (0 .. $ $ Column_lengths {$ column_lengths [$ i] & gt; ($ max_lengths [$ i] || 0)) {$ max_lengths [$ i] = $ column_lengths [$ i];}}} Print " Maximum lens for each field: @Max_Lambi \ n ";  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -