onyourmark
asked on
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\LPTH W>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.
C:\Users\Bill\Desktop\LPTH
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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi. Thanks. I am confused about this
foreach my $word (split) {
$words{$word}++;
What is split doing? It is not a function is it?
foreach my $word (split) {
$words{$word}++;
What is split doing? It is not a function is it?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. What is the other argument? What is it splitting?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I get this error
C:\Users\Bill\Desktop\LPTH
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
Open in new window
SampleForGraphing.csv