in reply to (tye)Re: DATA munging data
in thread DATA munging data


D'oh! i did forget that the OS would be important. However, i did say that a sample string of numbers (of which i would get characters) is:
   12 34 79 54 2

Maybe i should have said that these characters are:
   ^L " O 6 ^B
(that is, <ctrl>-L, double quote, capital "oh", the digit 6, and <ctrl>-B)

For the record, i'm using an i686 Linux Red Hat 6.1 box. The output code could look something like:

print map {chr} (12,34,79,54,75,8,2);
And the input code like:
@array = <DATA>;
In these examples, the problem exists, and binmode has not been used.

After further testing it seems that if the character for backspace comes up in the sequence, than it deletes the previous character in the string before getting into the array, which explains some of my results. i'll be doing further testing on reading character by character to see if it resolves that issue.

Sorry for the lack of information, i think i'm kind of known for it... :-{

,xnaht
jynx

Replies are listed 'Best First'.
(tye)Re2: DATA munging data
by tye (Sage) on May 11, 2001 at 01:50 UTC

    Yes, you said what you were trying to write which is the same as what "vi" saw. But you did not say what Perl and "tail" read back and how it was different.

    If you are expecting just plain "tail" to display binary data such as chr(8), then you are mistaken. If you are checking what Perl reads back via a simple     print $string; then you'll have the same problem.

    You are writing the data with something like:     print map {chr} @array; which can be rewritten as:     print pack "C*", @array; then you should be extracting the data after you read it with something like:     @array= unpack "C*", $string; You said you are using:     @array = <DATA>; which will split the data into "lines" based on the value of $/, so this probably isn't working too well. So you either need to set $/ (probably to undef) or use someting like read (or perhaps sysread).

    If you have a recent version of Perl, then another alternative is to set $/ to \1 (a reference to the scalar value 1) to tell Perl to read in fixed-length records of 1 byte each, but then you'll still have to unpack the value out of those 1-byte strings so I'd just do this:     @array= unpack "C*", do { local($/); <DATA> };

            - tye (but my friends call me "Tye")