Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

HOW TO MANIPULATE A TEXT FILE IN PERL

by fandaman (Initiate)
on Apr 01, 2014 at 14:24 UTC ( #1080573=perlquestion: print w/ replies, xml ) Need Help??
fandaman has asked for the wisdom of the Perl Monks concerning the following question:

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

Comment on HOW TO MANIPULATE A TEXT FILE IN PERL
Download Code
Re: HOW TO MANIPULATE A TEXT FILE IN PERL
by frozenwithjoy (Curate) on Apr 01, 2014 at 14:31 UTC

    Not quite sure what it is, but something tells me that you are trying to get people to do your homework assignment.

    EDIT: and now it's gone!

Re: HOW TO MANIPULATE A TEXT FILE IN PERL
by bigj (Monk) on Apr 01, 2014 at 17:55 UTC
    The first link of a google search could be a start :-)

    Greetings,
    Janek

Re: HOW TO MANIPULATE A TEXT FILE IN PERL
by Old_Gray_Bear (Bishop) on Apr 01, 2014 at 18:45 UTC
    One writes a Perl program that does the following:
    1. Open the input Text file and check for success
    2. Open the output Text file and checks for success
    3. Read the input Text file a line at a time
    4. Perform your manipulation of the text
    5. Write the manipulated line to the output Text file
    6. Read the next line of the input Text file. (Hint: use a Looping construct)
    7. Close input Text file and check for success
    8. Close output Text file and check for success
    9. Turn in you homework assignment
    10. Profit

    ----
    I Go Back to Sleep, Now.

    OGB

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1080573]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2015-07-06 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls