#!/usr/bin/env python
#=========================================================================================
# Read data from a file to a 2dim array - one record at a time
# Display the 2dim array 
# Write the 2dim array to another file 
#=========================================================================================
 
print("Content-Type: text/html \n")          #required http response header (w/ extra line)

fileIn  = '/home/sultans/data/cust_order.file'              	    #input file
fileOut = '/home/sultans/web/python/demo/data/cust_order2.file'     #output file

array2 = []                                          #2 dimensional array to store data

#========================================================================================
# read_file_into_array: read data from file into 2 dimensional array
#========================================================================================
def read_file_into_array(fileIn, array2):
 
    input  = open(fileIn,'r')                           #Open file for reading
    
    for rec in input:                                   #loop thru the data rows
        rec = rec.rstrip()                              #strip trailing newline
        row = rec.split('||')                           #split the record on ||
        (fn,ln,addr,flav,top,cc) = row                  #get each individual element (if need be)        
        array2.append(row)                              #append each row to the 2dim array
                
    input.close()                                       #close the file

#======================================================================================
# display_array: display the content of file as 2 dimensional array
#======================================================================================
def display_array(array2):
    
      for row in array2:                                #loop through all rows in 2dim array
            for element in row:                         #loop through all elements in a row
                  print(element, "\t", end='')
            print()

#======================================================================================
# display_array_html: display the content of file as 2 dimensional array in html table
#======================================================================================
def display_array_html(array2):
    
    print("<table border=1>")
    print("<tr bgcolor=tan><th>First Name<th>Last Name<th>Address<th>Flavors<th>Toppings<th>Credit Card</tr>")
    for row in array2:
        print('<tr>', end='')
        for element in row:
            print('<td>', element, '</td>', end='')
        print('</tr>')
    print("</table>")

#===================================================================================
# write_array_into_file: write data from 2 dimensional array into file
#===================================================================================
def write_array_into_file(array2, fileOut):

    output = open(fileOut,'w')                          #Open file for writing
    
    for row in array2:                                           #loop thru the data rows
        (fn,ln,addr,flav,top,cc) = row                           #breakup the row into a tuple
        rec  = fn +'||'+ ln +'||'+ addr +'||'+ flav +'||'+ cc    #concatenate all the data values
        rec += '\n'                                              #add a newline character

        output.write(rec)                               #write the record
                
    output.close()                                      #close the file

#===================================================================================
# main code
#===================================================================================
read_file_into_array(fileIn, array2)

array2.sort(key=lambda elem : elem[1])		#sort the 2dim array by elem[1] -> lastname

#display_array(array2) 
display_array_html(array2) 

write_array_into_file(array2, fileOut)



#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================