Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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
[kcott]: s/imput/input/
[Corion]: DateTime::Duration ? Personally,I convert stuff to seconds using Time::Piece or Time::Local and then add the seconds and then use POSIX::strftime to print them as hours again
[marto]: good morning all, TCIF
[gnosti]: I'm reading from ffmpeg lengths of video clips in H:M:S and needing to add them and format them back to H:M:S for feeding back to ffmpeg
[marioroy]: kcott Thanks
[Corion]: gnosti: I would cheat and use my approach instead of dealing with durations as objects, or a module
[Corion]: You could even do the calculation directly - my $duration = $H * 3600+ $M*60 + $S; strftime '%H:%M:%S', timelocal($duratio n)
[Corion]: (and that strftime call is largely superfluous too, converting back to H:M:S isn't hard either)

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2017-08-18 08:57 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (297 votes). Check out past polls.