#!/usr/bin/perl -w # program name: distanace.pl # # This program calculates the distance between any two botswana towns # It requires that two source files towns.txt and distance.txt be in the same # directory as this program # # First experiment with this program by supplying it with towns that are listed # in towns.txt. # # Adapt this code to suit your asnwers to the question in assigment 2 # Warning: There is no validation corrections or subroutines, it is upto you # to make sure you adapt this code to suit your solution. # # towns.txt format: town id|town name|location eg. 28|Ramatlabama|B # Distance.txt format: # This is a row-by-column table that stores distances between any pair # of towns. When distance is worked out, start with the smaller Town ID # as a row (or file line number in terms of script programming), # and a bigger number as a column (use normal array subscripts for rows). # There are 42 towns in all, this translates into a 42X42 table. Note that # these are true distances, not dummy values. # # STEPS: # # Step 1; # ======= # # Get two town names as input (from keyboard or file) # # Step 2: # ======== # # Work out the town codes from using towns.txt # # Step 3: # ======= # # Use the two town codes and distance.txt to determine the distance between them # # Author: Dr. Dimane Mpoeleng, Computer Science, CSI423 # ################################################################################ # Get two towns from the keyboard print "Type town 1:"; $town1=; chomp ($town1); print "Type town 2:"; $town2=; chomp($town2); ########################### # Work out the town codes ########################### open (TOWNS, "< towns.txt"); $town1code=999; # dummy values $town2code=999; while ($line=) { chomp($line); ($code, $town, $loc) = split(/\|/,$line); if ($town eq $town1) { $town1code = $code; } if ($town eq $town2) { $town2code = $code; } $loc=""; # do something with location so the compiler won't complain } close (TOWNS); ###################################### # Work out the distance between codes ###################################### #Remember how I warned you on how best to work out distance from the table? Read # Section 4.3 here: http://168.167.14.30/teaching/csi423/csi423_assignment2.htm # # When distance is worked out, start with the smaller Town ID as a row (or file # line number in terms of script programming), and a bigger number as a column # (use normal array subscripts for rows). There are 42 towns in all, this # translates into a 42X42 table. Note that these are true distances, not dummy # values. # #For example if the distance between Jwaneng (town1code = 8) and Francistown # (town2code = 3) is to be worked, 3 should be treated as a row and 8 as a # column to get 628 km. However swapping 8 and 3 by treating 8 as a row and 3 # as a column would yield xxxx. # # see how the table looks like here : # # http://168.167.14.30/teaching/csi423/distancetable.txt ########################################### open (DISTANCE, "< distancetable.txt"); @DistanceArray=; # the array will contain all lines of distance.txt # row by row unsplit, so each line has to be split # take out the row that matches the smaller towncode! # start with a small ID as a row then the bigger towncode as a column $row=""; # initialise row if ($town1code < $town2code) { $smallercode=$town1code; $biggercode =$town2code } else { $smallercode=$town2code; $biggercode =$town1code # note: town code start at 1, not 0, so we } # have already avoided reading in town # labes of distancetable.txt $row=$DistanceArray[$smallercode]; chomp($row); # remove newline # now split the row into an array. @RowArray=split(/\|/, $row); #get the distance $distance = $RowArray[$biggercode]; print "the two towns are : [$town1] and [$town2]\n"; print "their codes are : [$town1code] and [$town2code]\n"; print "distance between is : [$distance km]\n";