English 中文(简体)
Perl中的数据读数
原标题:Read chunks of data in Perl
  • 时间:2010-06-29 20:22:10
  •  标签:
  • perl

如果我没有限制,那么在Perl将一条线分为不同的长度,那是很好的办法。 我的数据按栏长度编排,因此第一个变量为1-4级,第二个变量为5-15级等。 每种变量都有不同的长度。

另一种方式是,根据扼杀中的位置,是否有某种方式使用分裂功能,而不是相应的表述?

感谢。

最佳回答

是的。 <http://perldoc.perl.org/Functions/un Pack.html>rel=“noretinger”>un Pack功能与处理固定-width记录非常合适。

Example

my $record = "1234ABCDEFGHIJK";
my @fields = unpack  A4A11 , $record;  # 1st field is 4 chars long, 2nd is 11

print "@fields";                       # Prints  1234 ABCDEFGHIJK 

第一个论点是模板,其中标有:unulation/code>,在田地开始和结束时。 第二个论点告诉它,它只是无包装的。

。 也可以听说在扼杀中忽略特征立场,具体指明无效,x。 模板<代码>A4x2A9>可用于忽略上述例子中的<代码>AB>。

perldoc -f Pack>>>>>>>>>> 深细节和实例<>。

问题回答

http://perldoc.perl.org/Functions/substr.html 方法:

my $first = substr($input, 0, 4);
my $second = substr($input, 5, 10);
# etc...

(如无包装方法一样,但如果你只把几个领域划归一,则在不咨询文件的情况下,子体更容易书写。)

您可使用<代码>()的功能提取数据,但可予以抵消:

$first = substr($line, 0, 4);
$second = substr($line, 4, 11);

另一种选择是定期表达:

($first, $second) = ($line =~ /(.{4})(.{11})/);




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签