Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Extra hash key when reading text file into hash

by lolindrath (Scribe)
on May 28, 2001 at 05:49 UTC ( #83679=perlquestion: print w/ replies, xml ) Need Help??
lolindrath has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, I hope this isn't too much of a newbie question. This program is supposed to keep track of my grades and figureout my GPA. I'm having problems reading the file into my hash so I can work with it. I get an extra hash key that looks like this: HASH(0x812d1f8)

The code:
#!/usr/bin/perl -w use strict; use CGI::Carp qw/fatalsToBrowser/; use CGI qw/:standard/; my $Query = new CGI; print $Query->header(); # #Globals # my %Semester = {}; my $TotalCredits = 0; open( FILE, "grade.dat" ) || die "Can't open grade.dat"; my @SemesterLine = <FILE>; close FILE; my $I = 0; foreach my $ClassLine ( @SemesterLine ) { $I++; print $I . "<br>"; chomp( $ClassLine ); my ( $SemesterName, $ClassName, $Grade, $Credits ) = split( /:/, $ +ClassLine ); if ( !( exists $Semester{$SemesterName}{'TotalCredits'} ) ) # chec +k to see if hash was already initalized { $Semester{$SemesterName}{'TotalCredits'} = $Credits; } else { $Semester{$SemesterName}{'TotalCredits'} += $Credits; } $Semester{$SemesterName}{$ClassName}{'Grade'} = $Grade; $Semester{$SemesterName}{$ClassName}{'Credits'} = $Credits; $Semester{$SemesterName}{$ClassName}{'Name'} = $ClassName; $TotalCredits += $Credits; } #Write a general report while( my ( $SemesterKey, $SemesterValue ) = each %Semester ) { print "SemesterKey: " . $SemesterKey . "<br>"; while ( my ( $ClassKey, $ClassValue ) = each %{ $SemesterValue } ) { print "ClassKey: " . $ClassKey . "<br>"; } }


The data file:
Spring 2001:COSC 210 - Object Oriented and GUI Programming:A:3 Spring 2001:COSC 220 - Applied Computer Programming:A:3


I put a counter into the read function to make sure that it wasn't looping too many times and reading in a new line. That wasn't the problem. At the bottom it prints out all the semester and class keys. The classes are read into the array correctly also. The output is below in case you want to look at that. Thanks everyone. I'm sure it's something simple and an experienced eye will pick it up quickly.
1 2 SemesterKey: Spring 2001 ClassKey: COSC 220 - Applied Computer Programming ClassKey: COSC 210 - Object Oriented and GUI Programming ClassKey: TotalCredits SemesterKey: HASH(0x812d1f8)


--=Lolindrath=--

Comment on Extra hash key when reading text file into hash
Select or Download Code
(ar0n: drunk) Re: Extra hash key when reading text file into hash
by ar0n (Priest) on May 28, 2001 at 05:55 UTC
    replace
    my %Semester = {};
    with
    my %semester = ();


    ar0n - under way too much influence of alcohol ]

Re: Extra hash key when reading text file into hash
by wog (Curate) on May 28, 2001 at 06:01 UTC
    If you looked at your server log or whereever your STDERR is going to, you should have noticed this:
    =-> perl -we 'my %hash = {}' Reference found where even-sized list expected at -e line 1. =->

    So check those logs or run the script on a shell to check for errors. It saves time.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://83679]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-08-28 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (259 votes), past polls