Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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
[muthusathish]: Hi All i need a help
[Eily]: hello muthusathish, we have many helps here
[muthusathish]: i have a perl based dll, how to decompile
[hippo]: :-D
[Eily]: you mean a dll where the SW was written in perl? That sounds unlikely
[muthusathish]: please help me regarding this
[hippo]: Easier just to go and look at the source, no?
[Eily]: mostly because perl isn't compiled in the usual sense of the meaning, you can't execute the opcodes directly, you need the perl engine
[muthusathish]: i dont have the environment too, but i need to get the logics so that i can re-wite
[Eily]: what makes you think your dll is "perl based" ?

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2018-02-23 09:10 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (301 votes). Check out past polls.