in reply to Re: String manipulation
in thread String manipulation

Ok, I understand what you did there but there is more to this I think. let me show the code that reads and saves the data:
my $sock = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port, Proto => "tcp",) or die "Cannot connect to PBX on address $host port $port: $!"; while (<$sock>) { print; # print to screen as well to show raw data and connection chomp($_); open(DAT,">>$filename") || die("Cannot open smdr file"); print DAT $_; close(DAT);
As you can see I print the string to screen before I save it to file and on the screen it looks perfect, everything lines up fine etc with no "^@" characters at all ever which makes me believe that this is not actually a string as such but some control character. That being the case do you think the code you provided will still work? I will try it and let you know.

Replies are listed 'Best First'.
Re^3: String manipulation
by GrandFather (Saint) on Aug 07, 2007 at 21:18 UTC

    Looks like the "keep alive" is a null character (ASCII value 0).

    while (<$sock>) { s/^\0+//; # Remove leading null characters print; # print to screen as well to show raw data and connectio +n chomp($_);

    should fix the problem.


    DWIM is Perl's answer to Gödel
      Thanks, moritz indicated that this was a "not so safe" way of doing this, any suggestions of a safer means?
        To be clear: moritz was talking about "removing all initial non-whitespace characters", and saying that it would be "not so safe" to do that -- and he would be right, in the sense that sometimes, initial non-whitespace characters might not be garbage, and it would be bad to remove them.

        GrandFather has proposed a different approach: just remove null bytes. There is nothing "unsafe" about this -- null bytes carry no relevant information, and they just get in the way. Deleting null bytes from your strings is a Good Thing.

        What moritz suggested was stripping any non-whitespace characters. The regex I suggested just removes leading ASCII null characters. For the task at hand that should be completely safe.

        I strongly recommend that you learn about Perl regular expressions. See perlretut, perlre and perlreref. A browse around the Tutorials section here is likely to pay off too.


        DWIM is Perl's answer to Gödel