error in code

ok.. so this is what i got for my code right now guys... when i add the last if statement
                        if(!Check($value, @POptions)){
                              delete $ghash{$value} or die "$!";
                        }

it says that the hash %ghash is empty when i try to print it.. do you guys have any idea why this is happening?

use constant DefaultInput   => "DEFAULT.txt";
use constant UserInput   => "USERINPUT.txt";
use constant gInput => "INPUT.txt";
 
 
my @Default = Default();
my @POptions = pinputs();
my @Attributes = attributes();
my %ghash;
 
 
if(-e gInput){
	my @input = &readinput(gInput);
	
		%ghash = @input;
			while (my ($entry, $value) = each %ghash){
				if(!Check($entry, @Attributes)){
					delete $ghash{$entry};
				}
			
				if(!Check($value, @POptions)){
					delete $ghash{$value} or die "$!";
				}
						
		
		
		}
	}
 
 
 
print Dumper \%ghash;
 
sub readinput{
	my $UserInput = shift;
	my @link;
	open(my $fh, "<", $UserInput) or die "Coudln't open file $!";
	close($UserInput);
		while(my $line = <$fh>){
				next if $line =~ /^\s*(?:#|$)/;
				push(@link,split(/:/,$line));
					}close($fh);					
	
	return @link;
					
}
 
sub pinputs{
my @inputs = ("67","97","2","3");
return @inputs;
}
 
sub attributes{
my @attributes = ("BOOK", "YEAR", "EDITORIAL");
return @attributes;
}
 
sub Check{
	my ($value, @array) = @_;
		foreach my $element (@array)	
			{	
				if($element =~ /\Q$value\E/){
					return 1;
	      				}
				
			}
		return 0;
	}

Open in new window

cucugirlAsked:
Who is Participating?
 
Adam314Connect With a Mentor Commented:
Your readinput returns an array, which is saved to @input.  Then @input is used to set the hash %ghash, and @input is never used again.  You should probably skip using @input.

Then, you have this:
1  while (my ($entry, $value) = each %ghash){
2      if(!Check($entry, @Attributes)){
3          delete $ghash{$entry};
4      }
5      
6      if(!Check($value, @POptions)){
7          delete $ghash{$value} or die "$!";
8      }
9  }

On line 3 (as numbered here), you might end up (depending on the return value from the Check subroutine) deleting the key from %ghash.  Then, you might attempt to delete it gain on line 7 (as numbered here).  If it is already deleted, you probably want to go to the next value, so add:
    next;
after line 3 (as numbered here).
0
 
cucugirlAuthor Commented:
also when i put

                        if(!Check($value, @POptions)){
                              delete $ghash{$value} or die "$!";
                        }

it says the following error:
Bad file number at COPY.pl line 42.
0
 
Adam314Commented:
Remove this on line 38:
    close($UserInput);
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
cucugirlAuthor Commented:
adam if i do that it says this:

Died at COPY.pl line 42.
0
 
cucugirlAuthor Commented:
delete $ghash{$value} or die "$!"; that is line 42
0
 
Adam314Commented:
Can you attach the three text files this program uses (INPUT.txt, USERINPUT.txt, and DEFAULT.txt)?
0
 
cucugirlAuthor Commented:
these fails are just a snapshot... userinput will be longer
DEFAULT.txt
INPUT.txt
USERINPUT.txt
0
 
cucugirlAuthor Commented:
code is breaking up userinput where there is colon... default is just a masterlist of the files and input.txt is just a global input file
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.