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

I'd like to split a "text" file based on an unprintable record delimiter: \x0a. The input file is basically like this:
\x0a Record stuff goes here more of the same record here yet more record data. \x0a Next record starts here and continues on for a while and finally terminates. \x0a You get the idea. Please note that \x0a is a one-character ASCII code, not the string of characters '\x0a'. \x0a I probably can't rely on indents for record boundary checking, either.

--- Code snippet begins ---
use strict; # I'm not a total neanderthal $/ = /\x0a/; # also tried '\x0a' open IN, shift; my @records = <IN>; close IN; # number of records == 1 (globbed!)
I suspect that my $/ = /\x0a/ and $/ = '\x0a' fails because \x0a isn't text, right? Must I do an unpack(), or am I overlooking something?

Thanks,
rje

Replies are listed 'Best First'.
Re: Splitting on unprintables
by ChrisS (Monk) on Aug 13, 2003 at 16:54 UTC
    Try:
    $/ = "\x0a";
    Single-quoted strings don't use escape characters like "\n" or "\x".
Re: Splitting on unprintables
by hardburn (Abbot) on Aug 13, 2003 at 17:00 UTC

    Does the file contain the literal text of '\x0a', or is it just the hex value of an ASCII character in the file. If literal text, then $/ = '\x0a' should work.

    If its the ASCII value (0x0A == ASCII Line Feed), then I'm guessing you're probably getting this from a *nix system and are parsing it on something else. Try setting $/ = "\n" or maybe $/ = 0x0A.

    Also, remember that $/ does not contain regular expressions.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      maybe $/ = 0x0A.

      Uh, no. That's equivalent to $/ = 10. He should be using an escape within a double quoted string (as in $/ = "\0xa") or perhaps chr() (as in $/ = chr(0x0a)).

      -sauoq
      "My two cents aren't worth a dime.";