in reply to Count number of characters in a DATA block

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

Replies are listed 'Best First'.
Re^2: Count number of characters in a DATA block
by eyepopslikeamosquito (Archbishop) on Sep 05, 2022 at 06:32 UTC

      G'day eyepopslikeamosquito,

      I hadn't realised that was in a FAQ; perhaps it's not quite as secret as I'd thought.

      Regardless, it's not something you can unsee, and =()= always makes me inwardly cringe.

      — Ken