G'day Trudge,

You've used unnecessarily complicated code. Many will not understand the perlsecret, Goatse; those that do may wish they didn't. All you really need is:

$PipeCounter = y/|/|/;

If it matters to you, that's also far more efficient.

Furthermore, that huge block of sample data is fairly useless to us. It might be your real data, but it's far too long for most to be bothered reading. It also doesn't check for too few or too many fields nor any edge cases (e.g. blank lines, zero-width fields, and so on). This would have been better:

#!/usr/bin/env perl use strict; use warnings; while (<DATA>) { print "Line $. has ", y/|/|/, "\n"; } __DATA__ ||||| !|@|#|$|%| |1|2|3|4| |q|w|e|r|t y|u|i|o|p A|S|D|F|G|H Z|X|C|V|B|N|

Output:

Line 1 has 0 Line 2 has 5 Line 3 has 5 Line 4 has 5 Line 5 has 5 Line 6 has 4 Line 7 has 5 Line 8 has 6

From there, you can enhance your code; for instance, skip blank and comment lines, highlight lines with the incorrect number of fields, and whatever else is appropriate for you.

— Ken


In reply to Re: Count number of characters in a DATA block by kcott
in thread Count number of characters in a DATA block by Trudge

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.