i have four files calls.txt ,towns.txt, subscribers.txt ,distance.pl & distancetable.txt. the link for the files is: http://www.cs.ub.bw/teaching/csi223/ calls.txt is a text file in the format: date|time|duration|callee_cell|caller_cell|callee_location|caller_location| towns.txt is a text file in the format : town_id|town_name|location distancetable.txt 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. subscribers.txt has the format: cell_number|name|gender|year|home_town_id|email QUESTION: how can i modify the distance.pl such that it accepts a caller_cell and calculate the distance between where the caller is making his/her calls and his/her hometown(from subsribers.txt).the script should accepts the two names of towns in string format (a town where the caller is making a call from and his/her home town) then calculate the distance between them USING DISTANCES PROVIDED IN THE **distancetable.txt here is the code:
#!/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 t +he same # directory as this program # # First experiment with this program by supplying it with towns that a +re 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 up +to 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 p +air # of towns. When distance is worked out, start with the smaller Tow +n 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. No +te 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 be +tween them # # Author: Dr. Dimane Mpoeleng, Computer Science, CSI423 # ###################################################################### +########## # Get two towns from the keyboard print "Type town 1:"; $town1=<STDIN>; chomp ($town1); print "Type town 2:"; $town2=<STDIN>; chomp($town2); ########################### # Work out the town codes ########################### open (TOWNS, "< towns.txt"); $town1code=999; # dummy values $town2code=999; while ($line=<TOWNS>) { 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 t +able? Read # Section 4.3 here: http://168.167.14.30/teaching/csi423/csi423_assign +ment2.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, t +his # translates into a 42X42 table. Note that these are true distances, n +ot dummy # values. # #For example if the distance between Jwaneng (town1code = 8) and Franc +istown # (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 ro +w 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=<DISTANCE>; # the array will contain all lines of dis +tance.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 readi +ng 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";

Content above restored by GrandFather


In reply to HOW TO MANIPULATE A TEXT FILE IN PERL by fandaman

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.