in reply to How to get contents from an array inside a hash
There are quite a few bugs happily tucked away in that code. A few coding techniques will help find many of them:
Ok, that's enough to be going on with. Here's a somewhat "cleaned up" version of your code. It probably still doesn't work as you want, but at least a few gremlins have been shaken loose:
use strict; use warnings; use List::MoreUtils qw{ any }; my $cnt_records_read = 0; my $layout = <<'LAY'; StudentLastName,180,30,Y,,Y StudentFirstName,210,30,N,,Y StudentMiddleName,240,30,N,, DateOfBirth,270,8,Y,,Y Gender,278,2,Y,,Y Grade,280,3,Y,, LAY my %layout; my @params = qw(Name Position Length); open my $layoutFile, '<', \$layout; while (defined (my $line = <$layoutFile>)) { chomp $line; my ($name, $pos, $len, $use) = split ",", $line; next if $use ne "Y"; @{$layout{$name}}{@params} = ($name, $pos - 1, $len); } close $layoutFile; my @records; my @fieldNames = sort keys %layout; while (my $line = <DATA>) { push @records, {}; for my $param (@fieldNames) { my $field = $layout{$param}; my $data = substr $line, $field->{Position}, $field->{Length}; $records[-1]{$param} = $data; } } for my $record (@records) { for my $name (@fieldNames) { printf "%-30s %8s %6s %s\n", @{$layout{$name}}{@params}, $record->{$name}; } } __DATA__ 111E2000 + 111E2000 + 201457660112567001 EP015F + SSHS 12232006 +01120 00 0 000 0 111 0 0 + sumsoc12S EPEN SS HS + NNNNNNNNNN 0000091323 + Y 111E2000 + 111E2000 + 201457660212567002 EP025F + SSHS 12232006 +01120 00 0 000 0 111 0 0 + sumsoc12S EPEN SS HS + NNNNNNNNNN 0000091324 + Y 111E2000 + 111E2000 + 201457660312567003 EP035F + SSHS 12232006 +01120 00 0 000 0 111 0 0 + sumsoc12S EPEN SS HS + NNNNNNNNNN 0000091325 + Y
which prints:
DateOfBirth 269 8 12232006 Gender 277 2 01 Grade 279 3 120 StudentLastName 179 30 EP015F + DateOfBirth 269 8 12232006 Gender 277 2 01 Grade 279 3 120 StudentLastName 179 30 EP025F + DateOfBirth 269 8 12232006 Gender 277 2 01 Grade 279 3 120 StudentLastName 179 30 EP035F
|
|---|