• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

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

0
cucugirl
Asked:
cucugirl
  • 5
  • 3
1 Solution
 
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
 
cucugirlAuthor Commented:
adam if i do that it says this:

Died at COPY.pl line 42.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
Adam314Commented:
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now