Skip to content

Instantly share code, notes, and snippets.

@thinkhy
Created October 28, 2012 01:27
Show Gist options
  • Save thinkhy/3967122 to your computer and use it in GitHub Desktop.
Save thinkhy/3967122 to your computer and use it in GitHub Desktop.
using unpack to deal with fix-width data
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 } = unpack $format, $_;
push @names, \%hash;
}
use Data::Dumper::Names;
print Dumper( \@names );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment