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=--

In reply to Extra hash key when reading text file into hash by lolindrath

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.