I need to make this into a multi-dimensional array. I know I need to change presArray = Table1Builder(60, "STR") to presArray = Table1Builder(60,3, "STR")

I am not sure how to load it. Do I need three load statements? Help Please!

# Import string to use split function.
import string

# Imports TableBuilder  for building the array
from Table1Builder import Table1Builder

def main():
    # Creates variable for .txt file.
    inFileName  = "presidents.txt"
    # Opens input files
    inFile = open(inFileName, "r")

    # Creates presArray
    presArray = Table1Builder(60, "STR")
    
    # Loop to LOAD the array.  @@@@@@@@@@@@@@@@@@
    ptr = 0
    for nextRecord in inFile:
        # Creates variables for data seperated by comas.
        name, years, age = string.split(nextRecord, ",")

        # Removes leading blank space from variable...years.
        years = years[1:]
        # Removes '\n' character from variable...age.
        age = age[1:-1]

        # LOAD  name  in the ARRAY ---------------
        presArray(ptr, name)
                
        # increment the array pointer
        ptr = ptr + 1

    # Loop to WRITE the array.  @@@@@@@@@@@@@@@@@@
    # NOTICE:  we are using the ptr that counted our input records.
    for indx in range(ptr):
        # Display the fields.
        print presArray(indx)

    # Close the files.
    inFile.close()

    # Shows data was saved.
    print
    print 'Data was read from', inFileName



    
# Call the main function.
main()

Recommended Answers

All 3 Replies

Using the words array and pointer in your code make me think that this is C++ or something.

Anyways, what is this Table1Builder? I'm not understanding the question.

I need to change presArray = Table1Builder(60, "STR") to presArray = Table1Builder(60,3, "STR")

You would first have to change the Table1Builder code so it would accept another arg. No offense, but if you are asking this question then you do not want to start changing code.

So you have 2 options that I can see.
1. Use Table1Builder(180, "STR") and have some code to use this like a (60,3) array
2. Call the Table1Buider(60, "STR") function 3 times and store each returned value in a container, again with code to process this so it looks like an array.

You would also have to change these corresponding statements that use presArray
presArray(ptr, name) ## is this doing what you think?
print presArray(indx) ## may have to be modified

Finally, use strip instead of slicing:
# Removes leading blank space from variable...years.
years = years[1:] becomes years = years.strip()
# Removes '\n' character from variable...age.
age = age[1:-1] becomes age = age.strip()
Python is a civilized language so behaves well.

You would first have to change the Table1Builder code so it would accept another arg. No offense, but if you are asking this question then you do not want to start changing code.

So you have 2 options that I can see.
1. Use Table1Builder(180, "STR") and have some code to use this like a (60,3) array
2. Call the Table1Buider(60, "STR") function 3 times and store each returned value in a container, again with code to process this so it looks like an array.

You would also have to change these corresponding statements that use presArray
presArray(ptr, name) ## is this doing what you think?
print presArray(indx) ## may have to be modified

Finally, use strip instead of slicing:
# Removes leading blank space from variable...years.
years = years[1:] becomes years = years.strip()
# Removes '\n' character from variable...age.
age = age[1:-1] becomes age = age.strip()
Python is a civilized language so behaves well.

Thanks for the input! I totally forgot I needed to change the table1builder. It is something our teacher created for us to start out with. I have it now though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.