#!/usr/local/bin/perl -w #formerly class4A.pl or ex4-2.pl #open and read in file with GI numbers unless(@ARGV==1) {die "please provide name of the file in the command line!!\n";} $filename=$ARGV[0]; #the file to be read contains a list of GI numbers #could need a check for contents open(IN, "< $filename") or die "cannot open $filename:$!"; #again, the program will exit, if it cannot open the file #(good against typos in the filename) @gi=(); #initialized an array where I will store GI numbers while(defined($line=)){ #populate array @gi with GI numbers chomp($line); if ($line!='') {push(@gi,$line)}; } close(IN); # Done with input########################## # determine number of GI numbers read! $number_gis=scalar (@gi); print "\nI read in $number_gis GI numbers\n\n"; ###################################### # task 4.2 : # # HOW MANY DUPLICATES PER GI NUMBER? # ###################################### %gi_hash=(); foreach (@gi) #populate gi_hash with key=gi-number and value how often encountered { $gi_hash{$_} += 1 ; #Hash contains Key: gi number Values: how often did it occur? } @gi_names = sort(keys(%gi_hash)); #assigns keys to an array $number_gis=@gi_names; # determines number of different keys, i.e different GI numbers print "I read $number_gis different gi numbers\n\n"; #next loop prints out GI numbers and how often they occured #foreach (@gi_names){ # print "$_ occurred $gi_hash{$_} times\n"; # } ######################################################### # sort on values numerically, then on keys @sorted_by_value = sort by_value (keys (%gi_hash)); sub by_value { $gi_hash{$a} <=> $gi_hash{$b} or $a <=> $b #if the values are the same, then sort ascibethically (cmp) or numerically (<=>) on the keys } # defines the order smaller befor larger (a before b) #####Next loop prints sorted keys and values foreach (@sorted_by_value) { printf "GI number %-9d occurred %4d times\n", $_, $gi_hash{$_}; } # exit; ################################ # task 4.3: #count how many unique GI numbers are in the list, i.e. count number of elements #in our %gi_hash hash. ############################### $unique_count=0; %gi_unique=(); foreach (@gi_names) # could have used @gi { if ($gi_hash{$_}==1) #if the value is exactly 1 { $unique_count++; #increase the uncique counter $gi_unique{$_}=1 # }; }; printf "\n%3d gi numbers occurred only once.\n\n", $unique_count ; #print unique GI numbers @gi_unique_array = sort(keys(%gi_unique)); print "\nThe following $unique_count GI numbers occurred once:\n"; foreach (@gi_unique_array){ printf "%9d \n", $_ ; } print "\n\n"; #### fine ###