Global symbol

Hi. I am getting the following error. It has to do with the globalness of the %hash. How can I fix that?

C:\Users\Bill\Desktop\LPTHW>perl freqTable.pl sampleForGraphing.csv
Variable "%hash" is not imported at freqTable.pl line 45.
Global symbol "%hash" requires explicit package name at freqTable.pl line 45.
Execution of freqTable.pl aborted due to compilation errors.


#!/Perl/bin/perl
use strict;
use warnings;

# create a list of files or just one file.
#this time I will just use one and so I will read it in from the command line.


# create a sub to read a list of files.
# but from within there I will have to call another function called read_line and so I have to
#define it before this one I think.

sub read_line { #this sub will be called from the other sub and the other sub will pass it
				# a line of text to work on. So this one will capture that line with shift;
				#but actually shift; will be called from within the split function and the 
				#result of the split function will create a list.
		our %hash;  #I think the fact that this is global is very important
					#because if not you would only get a frequency table for one line.
		
		my @list = split " ", shift;  #note: I think this is the same as 
									  # my @list = split(" ", shift);  ???Maybe.
		foreach my $word (@list) {
			$hash{$word}++;
			}
}

sub print_hash {
				our %hash = shift;
			while((my $key, my $value) = each( %hash)) {     
			
			print $key, $value;
			 }

}
sub read_file {
#I will call this file with @ARGV as the argument list.
#The only argument I need is the file name and so:
my $fileName = shift;
# Now you need to open the file
open (FILE, $fileName) || die "couldn't open $fileName";

while( my $line = <FILE>) { #do something.. I will use another subroutine to tell it what to do.
	read_line $line;
	}
print_hash %hash;
}

# so now both functions have been defined. Now we need to call the "first" one (the outside one).
read_file @ARGV;

Open in new window

onyourmarkAsked:
Who is Participating?
 
TintinConnect With a Mentor Commented:
Declare any globals just once.

Do this at the beginning of the script.
0
 
TintinConnect With a Mentor Commented:
Although, in your case, there's no need to use 'our'.  'my' is sufficient.
0
 
TintinConnect With a Mentor Commented:
In the code you posted, are you using subroutines as a learning exercise as there's no real point in using them for what you are doing.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
onyourmarkAuthor Commented:
Hi. I am attaching the file I tried the code on. I declared %hash just once now. I have also attached the new code.

I get this error

C:\Users\Bill\Desktop\LPTHW>perl freqTable2.pl sampleForGraphing.csv
Odd number of elements in hash assignment at freqTable2.pl line 29, <FILE> line 150.
Use of uninitialized value $value in print at freqTable2.pl line 32, <FILE> line 150.
Army?,Colombo
#!/Perl/bin/perl
use strict;
use warnings;

# create a list of files or just one file.
#this time I will just use one and so I will read it in from the command line.


# create a sub to read a list of files.
# but from within there I will have to call another function called read_line and so I have to
#define it before this one I think.

my %hash;
sub read_line { #this sub will be called from the other sub and the other sub will pass it
				# a line of text to work on. So this one will capture that line with shift;
				#but actually shift; will be called from within the split function and the 
				#result of the split function will create a list.
		#our %hash;  #I think the fact that this is global is very important
					#because if not you would only get a frequency table for one line.
		
		my @list = split " ", shift;  #note: I think this is the same as 
									  # my @list = split(" ", shift);  ???Maybe.
		foreach my $word (@list) {
			$hash{$word}++;
			}
}

sub print_hash {
				%hash = shift;
			while((my $key, my $value) = each( %hash)) {     
			
			print $key, $value;
			 }

}
sub read_file {
#I will call this file with @ARGV as the argument list.
#The only argument I need is the file name and so:
my $fileName = shift;
# Now you need to open the file
open (FILE, $fileName) || die "couldn't open $fileName";

while( my $line = <FILE>) { #do something.. I will use another subroutine to tell it what to do.
	read_line $line;
	}
print_hash %hash;
}

# so now both functions have been defined. Now we need to call the "first" one (the outside one).
read_file @ARGV;

Open in new window

SampleForGraphing.csv
0
 
TintinConnect With a Mentor Commented:
Here's the version that uses no subroutines (which for this purpose makes more sense) and proper formatting (makes it much easier to read)


use strict;
use warnings;

my %words;

foreach my $filename (@ARGV) {
  open my $FILE, '<', $filename or die "Can not open $filename because $!\n";

  while (<$FILE>) {
     foreach my $word (split) {
        $words{$word}++;
     }
  }

  close $FILE;

  while ( my ($key,$value) = each %words) {
     print "key is $key  value is $value\n";
  }
}

Open in new window

0
 
onyourmarkAuthor Commented:
Hi. Thanks. I am confused about this
foreach my $word (split) {
        $words{$word}++;

What is split doing? It is not a function is it?
0
 
TintinConnect With a Mentor Commented:
split is a builtin Perl function.

By default, split will split a string on whitespace.

0
 
onyourmarkAuthor Commented:
Thanks. What is the other argument? What is it splitting?
0
 
TintinConnect With a Mentor Commented:
split takes up to three parameters

       split /PATTERN/,EXPR,LIMIT
       split /PATTERN/,EXPR
       split /PATTERN/


Let's look at some examples.

my $string='one two three';
my @array = split ' ',$string;

this would split $string based on a space, so @array would end up with three elements, ie:

$array[0]  would contain: one
$array[1]  would contain: two
$array[2]  would contain: three


does that help?

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.