Two things need to note for unpack in Perl

Two things need to note for unpack in Perl,第1张

概述In item 116 of the book  "Effective Perl", author demonstrates how to use unpack to process fix-width columnar data. I tried to write a toy code for practice,  and found two things need to note. First

In item 116 of the book  "Effective Perl",author demonstrates how to use unpack to process fix-wIDth columnar data. I trIEd to write a toy code for practice, and found two things need to note.

First thing, MUST use  single quote '  before and after the format string, or you'll get unexpected result, because @xx can be parsed as an array in Perl if enclosed within double quote.

Second thing, format @xxx makes unpack move to specifIEd position,NOTICE here unpack will work from position xxx+1,not position xxx.


Code is as below.

      my     $format     =     'A2 @4 A10 @16 A6 @24 A*'    ;        
my $string = <<'ColUMMNAR'; ID First name MIDdle Last name 1 brian d foy 2 Joshua McAdams 3 Joseph N Hall ColUMMNAR
open my ( $fh ), '<' , \ $string ; my @headers = unpack $format , <$fh> ; my @names ; while ( <$fh> ) {      my %hash ;      @hash { @headers } = $_ ;      push @names ,128)">%hash ; }
use Data::Dumper:: names ; print Dumper ( \ @names );


Update:  I should say above format  of unpack in Perl is very similar with REXX.  Moreover,REXX is older than Perl,so MAYBE Perl community learned the pattern from REXX.

总结

以上是内存溢出为你收集整理的Two things need to note for unpack in Perl全部内容,希望文章能够帮你解决Two things need to note for unpack in Perl所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1293092.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存