#!/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 (){ 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; #split on colons $s1{animal_id} = 0; $s2{animal_id} = 0; if ($s1{$animal_id} eq undef){ #if there is no value for $s1{animal_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 station 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";