Here is some food for thought :)
Just say no to globals, esp fake globals, no to subroutines that don't take arguments, no to subroutine prototypes and forward declarations , and no to return $errorHit, and no to overly wordy and long variable names
Say yes to warnings, yes to subroutines that take arguments, yes to subroutines that return data values (not magic number error indicators), yes to subroutines that die on error, yes to concise and clear variable names
Maybe
Main( @ARGV ); exit( 0 ); sub Main { my $activity = { ... }; my $fieldLength = measureFieldLengths( $activity ); $fieldLength and writeOutHashrefs( $activity, $fieldLength ); }
Maybe see Re: What are the most basic, generic aspects of programming?, Re: Perl Best Practices for naming variables, About the use of the plural form for the name of variables
## $allActivity_hashref $allActivity_fieldLength_hashref ## $allActivity_href $allActivity_fieldLength_href ## $activity_href $activity_fieldLength_href ## $activity_href $activityFieldLength_href ## $activity_href $fieldLength_href ## $activity $fieldLength ## $activity $activityLength ## %activity %activityLength #~ activity-of-'1234-5678' #~ activity-of-'peter'
Maybe
#!/home/gnu/bin/perl -- ## ## ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr +-ce -nibc -i=4 -pt=0 "-nsak=*" #!/home/gnu/bin/perl -- use strict; use warnings; use autouse 'Data::Dumper' => qw(Dumper); Main( @ARGV ); exit( 0 ); sub Main { my $activity = { '1234-5678' => { 'Address' => "656 Poplar", 'City' => "Monroe", 'State' => "WI", 'Zip' => "87654", 'Phone' => "444-555-6666" }, '5757-4968' => { 'Address' => "656 Poplar", 'City' => "Nightmare", 'State' => "ND", 'Zip' => "56532", 'Phone' => "777-8888" } }; my $fieldLength = {}; eval { measureFieldLengths( $activity, $fieldLength ); 1; } and eval { writeOutHashrefs( $activity, $fieldLength ); }; warn Dumper( $activity, $fieldLength ); } ## end sub Main sub measureFieldLengths { my( $allActivity_hashref, $allActivity_fieldLength_hashref ) = @_; print "entering measureFieldLengths\n"; measureFieldLength( "ACTIVITY", $allActivity_hashref, $allActivity_fieldLength_hashref ); print "exiting measureFieldLengths\n"; } sub measureFieldLength { my( $tableName, $tableName_hashref, $tableName_fieldLength_hashref + ) = @_; print "entering measureFieldLength\n"; print "--inside measureFieldLength--input --" . Dumper( $tableName_hashref ); for my $Id ( sort { $tableName_hashref->{$a} <=> $tableName_hashref->{$b} } keys( %$tableName_hashref ) ) { my $row_hashref = $tableName_hashref->{$Id}; for my $field_name ( sort { $row_hashref->{$a} <=> $row_hashref->{$b} } keys( %$row_hashref ) ) { my $field_valu = $row_hashref->{$field_name}; $tableName_fieldLength_hashref->{$Id}->{$field_name} = 0; $tableName_fieldLength_hashref->{$Id}->{$field_name} = length( $field_valu ) if( defined( $row_hashref->{$field_name} ) ); } } print "--inside measureFieldLength--output --" . Dumper( $tableName_fieldLength_hashref ); ## die "errorHit..."; print "exiting measureFieldLength\n"; } ## end sub measureFieldLength sub writeOutHashrefs { my( $allActivity_hashref, $allActivity_fieldLength_hashref ) = @_; print "entering writeOutHashrefs\n"; print "--inside writeOutHashrefs --input --" . Dumper( $allActivity_fieldLength_hashref ); writeOutHashref( "ACTIVITY", $allActivity_fieldLength_hashref ); print "exiting writeOutHashrefs\n"; } ## end sub writeOutHashrefs sub writeOutHashref { my( $tableName, $tableName_hashref ) = @_; print "entering writeOutHashref\n"; for my $Id ( sort { $tableName_hashref->{$a} <=> $tableName_hashref->{$b} } keys( %$tableName_hashref ) ) { my $row_hashref = $tableName_hashref->{$Id}; print $Id. "\n"; my $outline = $Id; for my $field_name ( sort { $row_hashref->{$a} <=> $row_hashref->{$b} } keys( %$row_hashref ) ) { my $field_valu = $row_hashref->{$field_name}; $outline = $outline . join( '=', $field_name, $field_valu +); } print $outline. "\n"; } print "exiting writeOutHashref\n"; } ## end sub writeOutHashref __END__
Maybe $key and $val instead of $field_name and $field_valu
Maybe
sub printTable { ... printRow( $table->{ $Id } ); } } ## end sub printTable
Or maybe putting the important word first
sub TableWrite { ... RowWrite( $table->{ $Id } ); } } ## end sub TableWrite
Or to distinguish between packages and subs (packages CapsFirst subs lowerFirst )
sub tableWrite { ... rowWrite( $table->{ $Id } ); } } ## end sub tableWrite
In reply to Re: no visibility to global hashref ?!?
by Anonymous Monk
in thread no visibility to global hashref ?!?
by kerchunk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |