Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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)


Replies are listed 'Best First'.
(ar0n: drunk) Re: Extra hash key when reading text file into hash
by ar0n (Priest) on May 28, 2001 at 05:55 UTC
    my %Semester = {};
    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://83679]
Approved by root
[TCLion]: Good Morning Monks, I am parsing logs and some date time formats are inconsistent. I am trying to make output all same format. What is the best way to convert a month(word) to month(number)?
[Corion]: TCLion: I'm not aware of a better way than manually building the hash, as all strptime incarnations I know have nasty edge cases
[davido]: And remember; time is hard. :) As soon as you think, I've got this, you'll discover that time is harder than that. :)
[1nickt]: TCLion are you parsing the dates with DateTime or another tool?
[1nickt]: I note DateTime::Format:: Flexible, announced immediately below this box in the CPAN feed

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2017-03-23 14:42 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (287 votes). Check out past polls.