Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I correctly calculate the lengths of fields in a CSV dcoument using Perl?
    primarykey
    data
    text
    <p>I have a datas et and like to do a simple <code>while</code> operation with a Perl script. Here is a small extraction from the dataset:</p> <blockquote> <p>"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,"AGO","Angola","0.00",6.79,"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","Burkina 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</p> </blockquote> <p>The script should count the length of every <code>,</code> separated field and store the highest values into an array.</p> <p>However, the saving doesn't work properly. Here is a part of the code:</p> <pre><code>@maxl = map length, @terms; while(`&lt;INFILE&gt;`) { $_ =~ s/[\"\n]//g ; @terms = split/$sep/, $_; @lengths = map length, @terms; for($k = 0, $k &lt;= $#terms, $k++) { if($lengths[$k] &gt; $maxl[$k]) { $maxl[$k] = $lenghts[$k]; } } print "@lengths\n"; } </code></pre> <p>Now the <code>@maxl</code> uses an earlier part from the code where it uses the second line of the dataset. When I use a <code>print</code> command just to see the values of the <code>@maxl</code> operation i get:</p> <blockquote> <p>1 3 7 4 4 4 4 1 1 5</p> </blockquote> <p>In the <code>while</code> loop I used another <code>print</code> statement just to see the other values, I get:</p> <pre><code>1 3 6 4 4 4 4 1 1 4 1 3 5 5 4 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 4 1 1 1 1 3 8 4 4 4 4 1 1 4 1 3 10 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 4 1 1 4 2 3 5 4 4 4 4 1 1 4 2 3 5 4 4 4 4 1 1 4 2 3 8 4 4 4 4 1 1 4 2 3 5 4 4 4 1 1 1 4 </code></pre> <p>The fourth column eg has obviously values which are greater than 3. The <code>while</code> loop was supposed to save the greatest values and substitute those values into <code>@maxl</code>.</p> <p>What went wrong?</p> <hr> <p>...in the <code>for</code> loop the comma are wrong </p> <pre><code>for($k = 0, $k &lt;= $#terms, $k++) </code></pre> <p>however, after cleaning that up there still seems to be a problem...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload