Splitting data into columns using comma delimiter.
03:01
Populating the Data Table
Adding data to the table.
03:40
OutputTable Subprogram
Defining the OutputTable subprogram.
03:56
Data Formatting and Alignment
Formatting data for aligned column output.
04:02
Determining Max Column Width
Finding the maximum length for padding.
04:45
Formatting and Outputting Table
Iterating and formatting the output table.
Transcript
00:02
In this programme we'll read data from a CSV file and format it into columns for output to the screen.
00:09
Here is a sample of our data, that our programme is going to read.
00:13
You can see there are seven regions, one on each row, north west to the Isle of Wight.
00:19
Each region has a total millimetres of rain separated with a comma.
00:24
That's why the file is known as a comma separated file.
00:29
Data is presented in rows and columns with each row on a new line and each column separated by a comma.
00:36
It looks more like a table and would be stored in memory as a two dimensional array.
00:41
We've already learned about single dimensional arrays and lists, but now we learn that as well as a list of data items, we can also store a table of data too in a two dimensional array or list.
00:55
So, as always, we start with our boilerplate code.
00:58
In the main programme we'll declare a string that is a two dimension array.
01:03
We do this with the syntax square bracket comma, Square bracket data becomes what we read from a file named Data TXT and ReadData, is a subprogram we'll write in a moment.
01:18
Having read the data, we want to output the table.
01:22
So we'll write a subprogram called output table and pass it the data that we're going to read in.
01:29
So let's define the first of those subprograms, readdata, which will return our two dimensional array of strings.
01:37
So string, open bracket, comma, close bracket.
01:43
Identify the subprogram as readdata, and take our file name as a string parameter.
01:49
We have a rain collector gathering data in seven regions across the country, so we'll assign this to use in a loop later.
01:59
Next we need to declare our table string, open bracket, comma, close bracket, identify it as data and state it is a new string with two dimensions.
02:12
Numregions is the number of rows and 2 is the number of columns.
02:19
Now we need our stream reader to open a file that's our file name.
02:23
A for loop will iterate through the regions starting at zero, checking if the number of regions has been reached.
02:29
That would be seven and increasing the region by one each time.
02:35
Each row of data becomes a line read from the file.
02:38
We'll then trim off the hidden end of line markers at the end of each row in the file.
02:45
At the moment, our line of data is just one string data row and we need to split it into columns using the comma.
02:53
So string array datalist becomes the whole data row split by a comma.
03:01
Let's add that to our table.
03:04
So the table row for the region at, column zero is the data list.
03:09
Column zero.
03:11
In other words, we're adding the name of the region northwest into the first row and the first column of the table.
03:18
Remember, zero index is the first.
03:20
In programming, we can do the same thing for the rainfall.
03:25
That's the same row or region, but this time column one is the data list.
03:31
Column one.
03:33
Once all the regions have been read from the file, we can return the data table, which is now in memory.
03:40
Let's define the output table subprogram that will take the data table as a parameter.
03:46
This subprogram will format and output our data so that the data for the second column, that's the rainfall in millimetres, lines up with the other values in the column.
03:56
We need to pad the output with extra spaces before outputting the value of the rainfall.
04:02
To do this, we need to know which region has the most characters in it.
04:05
For example, Isle of Wight has 13 characters, but east only has four.
04:11
We can then pad the shorter names with spaces when the data is output.
04:15
To ensure the data in the first column is all the same width, a variable characters will hold what is known as the bester, or largest value.
04:25
We look at each region using a for loop and if the new longer region name is found, then characters is assigned to be the length of the name of that region.
04:35
Take a moment to study these lines of code.
04:38
They're very useful for many programmes where you want to know what the highest value is in an array or list.
04:45
Now that we know which region has the most number of letters, we can begin to format and output the data table.
04:52
We need to iterate through all the regions.
04:55
So another for loop a row in the table, we'll name TableRow is assigned to be the padded label, the first column of the row in the data table.
05:06
PaddedLabel is another subprogram which we'll pass the maximum number of characters to.
05:11
We'll come back to this subprogram, in a moment.
05:15
We want each column to be separated with a line.
05:19
We can use an ASCII pipe symbol or a vertical line for this.
05:23
So the table row is whatever it is now, plus the vertical line with a space either side.
05:29
For padding, the table row also needs to have the table column added to it to complete the output.
05:37
Output this to the screen and finally, outside the loop, output a blank line too.
05:44
Okay, so now padding the region name with spaces so that it matches the length of the characters for the longest name, making the data in the first column, all the same width.
05:55
Let's define the subprogram called paddedlabel that will need the label and the maximum number of characters as parameters to know how many spaces to add.
06:07
Return the label.
06:08
That's the name of the region plus the number of spaces equal to the characters minus 1.
06:15
So if we were padding east, then characters is 13, that's the length of Isle of Wight minus 4, that's the number of characters in East.
06:23
So we pad with 13 minus 4 and that's nine spaces.
06:29
That makes east the same width as Isle of Wight.
06:34
That's the programme written.
06:36
Let's give it a run.
06:37
Ignore the error.
06:38
I just put my file in the wrong folder.
06:41
With that fixed, you can see the CSV file has been read into a two dimensional array in memory and the output formatted to make it look like a table of data.