我有一行看起来像(没有新行,这是一行,我用_替换空格,否则它们被修剪):
46S990BZ6BRIG ___ 1381TRANSOCEAN_LTD ______________ BCALL_FEB00025000__1000000000000000000000000000000000000000B90002132015000000099999900161100000000000000007500111214111414121714100003000_H8817H100015012200005000000000010000000000000000000000009920202020150213__20 _________________________________________________ OV__0203P
使用unpack perl方法返回如下:
unpack("x49 A4", $line); # where $line is the above example line
返回:CALL
unpack("x68 A4", $line);
返回:0122
unpack("x238 A4", $line);
返回:2015
显然,列数与' x'之后给出的数字不匹配。在模板中,由于x238不等于第238列(' 0000'),我有2015年'在第251栏,而不是第238栏。另一方则相同。
请解释' x'之后给出的数字到底是怎样的?在模板工作中。
谢谢
答案 0 :(得分:0)
首先,您的数据不是您所说的。您提供的数据会产生所需的结果。
$ perl -E'
my $line = "46S990BZ6BRIG___1381TRANSOCEAN_LTD______________BCALL_FEB00025000__1000000000000000000000000000000000000000B90002132015000000099999900161100000000000000007500111214111414121714100003000_H8817H100015012200005000000000010000000000000000000000009920202020150213__20_________________________________________________OV__0203P";
$line =~ s/_/ /g;
say unpack("x238 A4", $line);
'
0000
也许您的实际数据包含不可打印的字符。或者也许有些空间实际上是标签。
$ perl -E'
$_ = "46S990BZ6BRIG___1381TRANSOCEAN_LTD______________BCALL_FEB00025000__1000000000000000000000000000000000000000B90002132015000000099999900161100000000000000007500111214111414121714100003000_H8817H100015012200005000000000010000000000000000000000009920202020150213__20_________________________________________________OV__0203P";
s/_/ /g;
s/LTD\K\s+/\t\t/; # If the spaces after LTD were tabs
say unpack("x238 A4", $_);
'
2015
如果要根据查看器扩展标签的方式提取字符,则需要在将字符串传递给unpack
之前以相同的方式将标签扩展到空格。