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;

In reply to Re: Hash search yields unexpected results by jwkrahn
in thread Hash search yields unexpected results by mattcsully

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.