Reputation: 929
I'm stuck on a seemingly trivial problem but not sure what is it that I'm missing. Need help.
I have a file that is delimited by the standard field separator (0x1f
) and record separator (0x1e
) characters. (https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text)
I don't need to parse out the fields but interested in getting the records.
I read about Perl's record separator special variable and tried using that to parse the file.
The file looks like this. ^
represents the field separator and ^^
represents the record separator (in vim). On sublime these will show up as the relevant hex codes.
ID^_NAME^_PARENTID^_Prov ID^_Pat_ID^_Another ID^_Program1^_Program2^_Status^_Date^_Reason^_Added^_Sn Length^_ze Reason^_StAge^_EnAge^_Notes^^NUMBER^_VARCHAR^_NUMBER^_ NUMBER^_NUMBER^_NUMBER^_VARCHAR^_VARCHAR^_VARCHAR^_DATE^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^_VARCHAR^^12^_40^_12^_^_12^_12^_200^_200^_12^_^_200^_1^_ 4000^_4000^_2000^_2000^_4000^^0^_^_0^_^_0^_0^_^_^_^_^_^_^_^_^_^_^_^^
Following is the code that I wrote to parse the records out. Issue is, whatever I do, the entire file is read into the $row scalar.
I initially assumed that perl expects the $/
to be set to a string type. Doing that also doesn't seem to work and I'm stuck.
Appreciate any help. Thanks.
#local $/ = sprintf("%s",chr("0xa"));
local $/ = chr(0xa);
open my $fh, "<", $file or die "$file: $!";
print("reading records\n");
while (my $row = <$fh>) {
print("Record:", $row, "\n");
}
Upvotes: 0
Views: 555
Reputation: 165208
You can use chr(0xNN)
, but it's simpler to write a hex character as "\xNN"
. A string containing record separator is "\x1e"
.
#!/usr/bin/env perl
use strict;
use warnings;
use v5.10;
my $file = shift;
open my $fh, "<", $file or die "$file: $!";
say "reading records";
local $/ = "\x1e";
while (my $row = <$fh>) {
say("Record:", join ",", split /\x1f/, $row);
}
Upvotes: 3