in reply to Hash search yields unexpected results
If you are going to use subroutines you should localize the data available to those subroutines, something like:
#!/usr/bin/perl # use strict; use warnings; use diagnostics; sub openControls { my ( $name ) = @_; # Opening the status files: print "In openControls\n open $name\n"; open my $FH, '<', $name or die "\nUnable to open [$name] file for +reading: $!\n\n"; return $FH; } sub closeControls { my ( $FH, $name ) = @_; # Close the status file: print "In closeControls\n close $name\n"; close $FH; } sub buildControlMap { my ( $FH ) = @_; unless ( fileno $FH ) { print "control_fh is closed\n"; return; } print "control_fh is opened and ready\n"; my %data; while ( <$FH> ) { chomp; my ( $key, $row ) = split /\|/; $data{ $key } = $row; } return %data; } sub printControlMap { my ( $data ) = @_; print "Print Process Control hash\n"; while ( my ( $control, $process ) = each %$data ) { print "[$control], [$process]\n"; } } sub checkControlMap { my ( $key, $data ) = @_; print "Look for control [$key] - "; return exists $data->{ $key } ? $data->{ $key } : print "WARNING [ +$key] was not found. - "; # If it's not found, return "1" } my $control_file = 'process.txt'; # Build and step through the control hash my $control_fh = openControls( $control_file ); my %process_control = buildControlMap( $control_fh ); printControlMap( \%process_control ); my @list1 = qw/ a1z a2x b3y b4w c5u c6t d7s d8r e0q e1p f2o f3m i4n +i5l k6k /; my @list2 = qw/ c5u b3y e1p c6t f3m a1z k6k i4n e0q f2o b4w d7s i5l +a2x d8r xyz /; for my $test ( [ 1, @list1 ], [ 2, @list2 ], [ 3, ' ' ] ) { my $number = shift @$test; print "TEST list $number #####################\n"; for my $key ( @$test ) { my $process = checkControlMap( $key, \%process_control ); print "checkControlMap( $key ) returned process [$process]\n"; } } closeControls( $control_fh, $control_file );
If you just want all of your data global you could do something like this:
#!/usr/bin/perl # use strict; use warnings; use diagnostics; my $control_file = 'process.txt'; # Opening the status files: print "In openControls\n open $control_file\n"; open my $control_fh, '<', $control_file or die "\nUnable to open [$con +trol_file] file for reading: $!\n\n"; print "control_fh is opened and ready\n"; my %process_control; while ( <$control_fh> ) { chomp; my ( $key, $row ) = split /\|/; $process_control{ $key } = $row; } print "Print Process Control hash\n"; while ( my ( $control, $process ) = each %process_control ) { print "[$control], [$process]\n"; } my @list1 = qw/ a1z a2x b3y b4w c5u c6t d7s d8r e0q e1p f2o f3m i4n +i5l k6k /; my @list2 = qw/ c5u b3y e1p c6t f3m a1z k6k i4n e0q f2o b4w d7s i5l +a2x d8r xyz /; for my $test ( [ 1, @list1 ], [ 2, @list2 ], [ 3, ' ' ] ) { my $number = shift @$test; print "TEST list $number #####################\n"; for my $key ( @$test ) { print "Look for control [$key] - "; my $process = exists $process_control{ $key } ? $process_contr +ol{ $key } : print "WARNING [$key] was not found. - "; print "\$process_control{ $key } returned process [$process]\n +"; } } # Close the status file: print "In closeControls\n close $control_file\n"; close $control_fh;
|
|---|