I am trying to format data in a file that is bombing at the printer.
The file has the following format:
************** 123456 ** B-009
ip address
SERVER 1
some server info [-snip-]
Data Criteria
Each record needs to have exactly 8 lines until the next row of
asterisks (that indicate new record start) Any less rows and the
printer bombs. The first and second records in the above example are
correct.
some records have trailing asterisks, as in first record.
each record has always four or five lines of chars.
Why do you want to send a "line count" oriented record
to the printer? Printers don't "bomb".
Sounds line you are monitoring an ascii printer port stream
to roll records into a database somewhere. Old hospitals used
to do this by running a real-time mainframe (or as400) print job that
prints this info (where the data can't be querried from incompatible
systems). I think I did this at some point in my past.
The first problem is that you rely on the end-of-record
to be a series of blank lines without having a strategy for the
beginning-of-record. The replies here key on that fact
that you emphisize. This is a "no-go" reliability factor.
If your pickup routine on the other end "keys" on that, then its
bad also.
You can't rely on the "end" to be the start of a new "begin".
You can only rely on the new "begin" and thats all. Otherwise,
a fals end could trigger a false-positive beiginning!
You can only rely on a "begin" trigger. Remember you are
writing this to pre-format that which is going to the printer.
Since your post printer program keys on line no's, you can't
get this middle step wrong. I assume you have no control over
the generation of the data file you are reading in.
You will have to qualify this block:
************** 123456 ** C-009
ip address
SERVER 1
some server info
You must key in on something in this line:
"************** 123456 ** C-009"
as the beginning of record. Since you assume
any contrast on this line has more of a chance
to be the beggining of record than any others.
And you have to validate anything else in the
record too (like ip) that its in the correct
spot and within range of what you would expect.
You should also have the logic to "reject" it
as a valid record if its outside the parameters
and not pass it on to the hard size/values/position
the printer expects.
Consider this as a controlled crash, where the
steering by the driver is "estimated" and without
guarante, to maneuver obsticals.
I'm going to quote Perly Girl's code here.
Not to critisize, but because she actually
DID exactly what you requested. No more and
no less. She was 100% right on. I don't think
it should be done this way (just read above).
This scenario is way too problematic and requires
a few more levels of error checking if it were
to be professional code. Code that approaches
Perl 1-liners is very disturbing. Its inherrently
dangerous because of its limited scope (and
caveats).
Streaming data has to be pulled off like train
"boxcars" when it passes into, and lines up
with the correct triggers. It must be qualified
by having the correct front/middle/rear and be
rejectable if not. Everybodys got thier own
level of patients. It just a matter of how much
agravation is acceptable. Dropped records
or mis-aligned boundries (corruption) may
be ok for what this is. Speed though is
NOT and never will be any kind of replacment
for accuracy and probablities of outcome.
- - - - - - - - - - - - - - - - - - - - - -
Remember is just an example (by Purl Gurl),
typically "how" things can go wrong (she
did exactly what you asked for and did it good!).
Sometimes you have to give a little more thought
thou when writing the specs --
--------------
don't recommend this
while (<DATA>)
{
do
{ $_ = "$_\n"; }
until ($_ =~ tr/\n/\n/ == 8); <--- hangs, see below until ($_ =~ tr/\n/\n/ >= 8);
print $_;
}
------------
__DATA__
************** 123456 ** B-009
ip address
SERVER 1
some server info
******************************
************** 123456 ** B-009
ip address1
ip address2
SERVER 1
some server info
Prints this:
-------------------------
************** 123456 ** B-009
ip address
SERVER 1
some server info
******************************
************** 123456 ** B-009
ip address1
ip address2
SERVER 1
some server info
__DATA__
************** 123456 ** B-009
ip address
SERVER 1
some server info
******************************
" " <-----
" " <-----
************** 123456 ** B-009
ip address1
ip address2
SERVER 1
some server info
Hangs the machine !!