Two things need to note for unpack in Perl
2012-10-28 09:47
405 查看
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 HallCOLUMMNAR
open my ($fh), '<', \$string;my @headers = unpack $format, <$fh>;my @names;while(<$fh>) { my %hash; @hash { @headers } = unpack $format, $_; push @names, \%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.
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 HallCOLUMMNAR
open my ($fh), '<', \$string;my @headers = unpack $format, <$fh>;my @names;while(<$fh>) { my %hash; @hash { @headers } = unpack $format, $_; push @names, \%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.
相关文章推荐
- You need perl module Net::SNMP to run check_openmanage in SNMP mode
- 使用VS进行工作流开发系列博客3-Developing Workflows in VS: Part 2 - Planning Your Workflow: Two Things to Keep in Mind
- need to set default value for Webservice's if it's used as datasource displayed in Moss
- Guid is not updated for cluster with specified cluster id; need to wait for hosts in this cluster to come up
- Developing Workflows in VS: Part 2 - Planning Your Workflow: Two Things to Keep in Mind
- Something I need to finish in following two months
- Top 10 Things to Be Thankful for in .NET
- 22.You need to create a table for a banking application. One of the columns in the table has the fol
- ten things to try in 2017 --- just for selt-motivation
- there are things you need to test for to get test coverage
- Top 10 Things You Need to Know in java 6 beta 2
- Things Need To Be Done in 2016
- How to trim and edit videos in Photos for OS X
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
- To SP or not to SP in SQL Server: an argument for stored procedures
- How to fix the issue that GEM_HOME and/or GEM_PATH not set issue for rvm in mac version 10.12
- Top 10 New Things You Need to Know About Java 6
- How to enable the Xdebug debugger in Zend Studio for Eclips
- Unable to load native-hadoop library for your platform... using builtin-java classes where applicab
- How to use next and last in Perl