Hello, I'm working on an assignment which requires me to read from a .log file, split it, and count how many times a certain ID went to a certain building. For example: a19:01-01-2009:s1 a31:01-01-2009:s2 a28:01-01-2009:s1 a41:01-01-2009:s1 I would split on the colons. Element one is the ID (For an animal) Element 2 is the date it visited a building (I don't use this value in my code) and the third element is the building the ID visited. I keep getting an error when I run my code
#!/usr/bin/perl -w use strict; use warnings; use diagnostics; my %s1 = (); #initialize hashes my %s2 = (); open FILE, "stations.log" or die "cannot open file stations.log:\, it +does not exist. $!\n"; #open file for reading while (<FILE>){ my $station_visits = $_; #initialize variable and set to the file +which was opened chomp ($station_visits); #CHOMP! my ($animal_id, $date , $station) = split/:/, $station_visits; #sp +lit on colons $s1{animal_id} = 0; $s2{animal_id} = 0; if ($s1{$animal_id} eq undef){ #if there is no value for $s1{anima +l_id}, set it to 0 $s1{$animal_id} = 0; } if ($s2{$animal_id} eq undef){ #if there is no value for $s2{animal +_id}, set it to 0 $s2{$animal_id} = 0; } #number of times animal visits station 1 if ($station eq 's1'){ #If the animal vists station 1, create, to + 0, and add 1. # if (not exists($s1{$animal_id})){ # $s1{$animal_id} = 0; #} $s1{$animal_id} = $s1{$animal_id} + 1; } else{# ($station eq 's2'){ #if (not exists($s2{$animal_id})){ #If the animal vists stati +on 2, create, to 0, and add 1. # $s2{$animal_id} = 0; # } $s2{$animal_id} = $s2{$animal_id} + 1; } } print "Animal Id\tStation 1\t Station 2\n"; foreach my $animal_id (sort keys (%s1)){ #for each hash, print values + into a table print "$animal_id \t\t $s1{$animal_id} \t\t $s2{$animal_id}\n";
The error I recieve is "Use of uninitialized value in string eq at analyze-frequencies.pl line 21, <FILE> line 283 (#1) Use of uninitialized value in string eq at analyze-frequencies.pl line 18, <FILE> line 284 (#1) Use of uninitialized value in string eq at analyze-frequencies.pl line 21, <FILE> line 284 (#1) Followed by the desired output : Animal Id Station 1 Station 2 a23 6 0 a37 12 4 a45 0 10 ... I am really unsure as to why I am getting this error. I thought I initialized my hashes and the $s1{animal_id} = 0; part too. I am at my wits end regarding this, I've spend quite a few hours and this is all I have. Thanks for taking the time to read my plea for help ; ;

In reply to Uninitialized errors when using 2 hashes. by Anonymous Monk

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.