Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's tempting to use Perl's <code>awk</code>-like auto-split option. There are 5 columns; three containing date and time information, then the size and then the name.</p> <p>The first version of the script that I wrote is also the most verbose:</p> <pre><code>perl -n -a -e '$total += $F[3]; $num++; END { printf "%12.2f\n", $total / ($num + 0.0); }' </code></pre> <p>The <code>-a</code> (auto-split) option splits a line up on white space into the array <code>@F</code>. Combined with the <code>-n</code> option (which makes Perl run in a loop that reads the file name arguments in turn, or standard input, without printing each line), the code adds <code>$F[3]</code> (the fourth column, counting from 0) to <code>$total</code>, which is automagically initialized to zero on first use. It also counts the lines in <code>$num</code>. The <code>END</code> block is executed when all the input is read; it uses <code>printf()</code> to format the value. The <code>+ 0.0</code> ensures that the arithmetic is done in floating point, not integer arithmetic. This is very similar to the <code>awk</code> script:</p> <pre><code>awk '{ total += $4 } END { print total / NR }' </code></pre> <p>First drafts of programs are seldom optimal — or, at least, I'm not that good a programmer. Revisions help.</p> <p>Perl was designed, in part, as an <code>awk</code> killer. There is still a program <code>a2p</code> distributed with Perl for converting <code>awk</code> scripts to Perl (and there's also <code>s2p</code> for converting <code>sed</code> scripts to Perl). And Perl does have an automatic (built-in) variable that keeps track of the number of lines read. It has several names. The tersest is <code>$.</code>; the mnemonic name <code>$NR</code> is available if you <a href="http://perldoc.perl.org/perlvar.html#Variables-related-to-filehandles"><code>use English;</code></a> in the script; so is <code>$INPUT_LINE_NUMBER</code>. So, using <code>$num</code> is not necessary. It also turns out that Perl does a floating point division anyway, so the <code>+ 0.0</code> part was unnecessary. This leads to the next versions:</p> <pre><code>perl -MEnglish -n -a -e '$total += $F[3]; END { printf "%12.2f\n", $total / $NR; }' </code></pre> <p>or:</p> <pre><code>perl -n -a -e '$total += $F[3]; END { printf "%12.2f\n", $total / $.; }' </code></pre> <p>You can tune the print format to suit your whims and fancies. This is essentially the script I'd use in the long term; it is fairly clear without being long-winded in any way. The script could be split over multiple lines if you desired. It is a simple enough task that the legibility of the one-line is not a problem, IMNSHO. And the beauty of this is that you don't have to futz around with <code>split</code> and arrays and read loops on your own; Perl does most of that for you. (Granted, it does blow up on empty input; that fix is trivial; see below.)</p> <h3>Recommended version</h3> <pre><code>perl -n -a -e '$total += $F[3]; END { printf "%12.2f\n", $total / $. if $.; }' </code></pre> <p>The <code>if $.</code> tests whether the number of lines read is zero or not; the <code>printf</code> and division are omitted if <code>$.</code> is zero so the script outputs nothing when given no input.</p> <hr> <p>There is a noble (or ignoble) game called 'Code Golf' that was much played in the early days of Stack Overflow, but Code Golf questions are no longer considered good questions. The object of Code Golf is to write a program that does a particular task in as few characters as possible. You can play Code Golf with this and compress it still further if you're not too worried about the format of the output and you're using at least Perl 5.10:</p> <pre><code>perl -Mv5.10 -n -a -e '$total += $F[3]; END { say $total / $. if $.; }' </code></pre> <p>And, clearly, there are a lot of unnecessary spaces and letters in there:</p> <pre><code>perl -Mv5.10 -nae '$t+=$F[3];END{say$t/$.if$.}' </code></pre> <p>That is not, however, as clear as the recommended version.</p>
 

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