in reply to String manipulation

The example input helps alot.

If you want to remove all instances of ^@, you could use a regular expression like this:

#!/usr/bin/env perl use warnings; use strict; while (<DATA>) { s/\^@//g; print; } __DATA__ 08/07 03:53P 00:00:46 130 9 4295794 08/07 03:56P 00:01:35 T001 001 ^@^ +@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ 08/07 04:13P 00:00:04 08/07 04:13P +00:00:02 T001 001
This produces the following output:
08/07 03:53P 00:00:46 130 9 4295794 08/07 03:56P 00:01:35 T001 001 08 +/07 04:13P 00:00:04 08/07 04:13P 00:00:02 T001 001
If this is not what you are asking, then please give us more of your input, and expected output.

Replies are listed 'Best First'.
Re^2: String manipulation
by bajangerry (Sexton) on Aug 07, 2007 at 20:39 UTC
    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.

      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?