I'm trying to build a utility that will create a character set, based on a pre-defined record layout. The code loads the layout into a hash, updates the character set for each field as it reads the input file, and then it's supposed to print the character set to a results file. Data::Dumper shows the expected character sets, but the printed output is blank for the character sets.

I have removed several comments and display statements to keep the size down, but wasn't sure if my problem might be higher in the code so have included it all. I believe the issue is in the "Format Printed layout" section.

Layout file example:

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,,

Input file example:

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

And the code...

#************************************* # Define external utilities #************************************* use strict; #use warnings; #use diagnostics; use List::MoreUtils qw{ any }; use Data::Dumper qw(Dumper); #************************************* # Assign the variables with the value passed to program #************************************* my $Layout = $ARGV[0]; my $File1 = $ARGV[1]; my $Results = $ARGV[2]; #************************************* # Initialize Global Variables #************************************* my $cnt_records_read = 0; my $null = ""; #************************************* # Open Files. #************************************* my ($layoutFile, $inputFile, $resultsFile); if (-e $Layout) { open($layoutFile, "<", "$Layout") or die("Could not open Layout +file: $!\n"); } if (-e $File1) { open($inputFile, "<", "$File1") or die("Could not open File 1. $ +!\n"); } open($resultsFile, ">", "$Results") or die("Could not open Results + File. Verify user has permissions for the results folder location. $ +!\n"); #************************************* # Create hash of layout file #************************************* my $cnt_all_fields = 0; my $cnt_omitted_fields = 0; my %layout=(); my $layout; my $layout_key = $null; my @layout_value = ""; my $layout_value = $null; my @chars=(); while (my $line1=<$layoutFile>) { chomp $line1; my @fields = split("," , $line1); if ( $fields[3] eq "Y" ) { $cnt_all_fields++; my $layout_key = $fields[0]; my $layout_value = { 'Field Name' => $fields[0] ,'Position' => $fields[1] - 1 ,'Length' => $fields[2] - 0 ,'Characters' => [ @chars ] }; $layout{ $layout_key } = [ $layout_value ]; } else { $cnt_omitted_fields++; } } # print Dumper %layout; #************************************* # Update Characters Array in Layout Hash #************************************* my $cnt_records_read_file1=0; my $char; my $chars; my @fld_detail; my @fld_chars=(); while ( my $line1 = <$inputFile> ) { $cnt_records_read_file1++; chomp($line1); foreach my $name ( keys %layout ) { foreach my $fld_detail ( @{ $layout{$name} } ) { my $fld_name = $fld_detail->{'Field Name'}; my $fld_position = $fld_detail->{'Position'}; my $fld_length = $fld_detail->{'Length'}; my @fld_chars = @{ $layout{$name}}{'Characters'}; my $field1 = substr($line1,$fld_position,$fld_length); for(my $pos = 0; $pos < $fld_length; $pos++) { my $char = substr($field1,$pos,1); if (any { $_ eq $char } @fld_chars) { } else { push @fld_chars, $char; $layout_key = $fld_name; $layout_value = { 'Field Name' => $fld_name ,'Position' => $fld_position ,'Length' => $fld_length ,'Characters' => [ @fld_chars ] }; $layout{ $layout_key } = [ $layout_value ]; } } } } } print Dumper \%layout; #************************************* # Format printed layout #************************************* foreach my $name ( keys %layout ) { foreach my $fld_detail ( @{ $layout{$name} } ) { my $fld_name = $fld_detail->{'Field Name'}; my $fld_position = $fld_detail->{'Position'}; my $fld_length = $fld_detail->{'Length'}; my @fld_chars = @{$layout{$name}}{'Characters'}; print @fld_chars; $fld_name = sprintf("%-30s",$fld_name); $fld_position = sprintf("%8s",$fld_position); $fld_length = sprintf("%6s",$fld_length); print $resultsFile "$fld_position $fld_length $fld_name @f +ld_chars \n"; } } #************************************* # Close Files #************************************* close($layoutFile); close($inputFile); close($resultsFile);

Dumper output:

$VAR1 = { 'StudentLastName' => [ { 'Length' => 30, 'Position' => 179, 'Field Name' => 'StudentLastName', 'Characters' => [ undef, 'E', 'P', '1', '9', '5', 'F', ' ' ] } ], 'SASID' => [ { 'Length' => 10, 'Position' => 159, 'Field Name' => 'SASID', 'Characters' => [ undef, '2', '0', '1', '4', '5', '7', '6', '9' ] } ], 'Grade' => [ { 'Length' => 3, 'Position' => 279, 'Field Name' => 'Grade', 'Characters' => [ undef, '1', '2', '0' ] } ], 'Gender' => [ { 'Length' => 2, 'Position' => 277, 'Field Name' => 'Gender', 'Characters' => [ undef, '0', '1' ] } ], 'DateOfBirth' => [ { 'Length' => 8, 'Position' => 269, 'Field Name' => 'DateOfBirth', 'Characters' => [ undef, '1', '2', '3', '0', '6' ] } ] };

And sample warnings. There are many variations of @fld_chars is undefined notifications.

Pseudo-hashes are deprecated at procFreq2.pl line 136, <$inputFile> li +ne 20. Use of uninitialized value in string eq at procFreq2.pl line 143, <$in +putFile> line 20. Pseudo-hashes are deprecated at procFreq2.pl line 186, <$inputFile> li +ne 20. Use of uninitialized value in print at procFreq2.pl line 188, <$inputF +ile> line 20.

In reply to How to get contents from an array inside a hash by gingera2z

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.