Singularlattice has asked for the wisdom of the Perl Monks concerning the following question:

I think you misunderstand my question. Reply from #1: yes, I've noticed the issue with reading from the handle twice. I spent a lot of time messing with the code trying to figure this out, and it got left there. My mistake. The point I am tryin to address is: why do I get a string of (seemingly) unrelated numbers when I try and do anything with the data read from the filehandle?? L

Replies are listed 'Best First'.
Re: Regex & reading from filehandles
by jwkrahn (Abbot) on Jan 28, 2009 at 13:12 UTC
    open $readhandle, "<", "Input.log"; open $writehandle, ">", "Output.log";

    You should always verify that the files opened correctly:

    open my $readhandle, '<', 'Input.log' or die "Cannot open 'Input.log +' $!"; open my $writehandle, '>', 'Output.log' or die "Cannot open 'Output.lo +g' $!";

    while(<$readhandle>) { $line = readline($readhandle);

    You are reading the first line into $_ and then reading the second line into $line so you only ever deal with half the lines in the file.   You probably want to work with every line of the file, like this:

    while ( my $line = <$readhandle> ) {

    $line = ~/(.)\n/;

    You are assigning the bitwise negation of the match operator to $line.   It looks like you intended to use the binding operator (=~) instead although that expression would then be in void context and would not modify or output anything.

Re: Regex & reading from filehandles
by leuchuk (Novice) on Jan 28, 2009 at 13:38 UTC
    Hello,

    I'm no real "perl monk" but I think I understand a little bit of the topic.

    First a few question:
    Do ALL your lines really like that?
    If yes: Take instead of a regex an "unpack" to break the lines into pieces.
    There are two reasons: Faster and easier to understand what is in the file. A regex dot says nothing about the structure at all. You will know when you have to change your script after having six month no look at it.

    Then: Am I right, the line looks like a set of hex-numbers? You can convert them back from a decimal number to a hex-number in your $writehandle->printf with the appropriate conversion sign.

    Does this help you to make your program working?

    Regards
    Leuchuk