[英]Perl record separator -
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. 我有一个由标准字段分隔符( 0x1f
)和记录分隔符( 0x1e
)字符分隔的文件。 ( https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text ) ( 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. 我阅读了有关Perl的记录分隔符特殊变量的信息,并尝试使用该变量来解析文件。
The file looks like this. 该文件如下所示。 ^
represents the field separator and ^^
represents the record separator (in vim). ^
代表字段分隔符, ^^
代表记录分隔符(在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. 问题是,无论我做什么,整个文件都被读入$ row标量。
I initially assumed that perl expects the $/
to be set to a string type. 我最初以为perl期望将$/
设置为字符串类型。 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");
}
You can use chr(0xNN)
, but it's simpler to write a hex character as "\\xNN"
. 您可以使用chr(0xNN)
,但是将十六进制字符写为"\\xNN"
更简单。 A string containing record separator is "\\x1e"
. 包含记录分隔符的字符串为"\\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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.