
-----------------------------------
Da_Big_Ticket
Mon Feb 27, 2006 8:58 pm

Comma - Delimited - File (CSV) reading issue
-----------------------------------
Ok so for this turing assignment which cervantes is doing with me, we are to enter precipitation data into a CSV file, which is data seperated by commas.

I am using a 2d array to store this information in, as the info is to be stored in a table (grid) system. Avoiding the 2D array is NOT an option.

I can get it to store ALL info in the 2D array and output it on screen, but my porblem is, as most of you would guess, getting rid of the commas, replacing them with spaces so they are easily read and the data is entered correct on the chart.

I have tried code something like this:

open : file, "f:/percipitation.csv", get
for a : 1 .. 7
    for b : 1 .. 5
        exit when eof (file)
        get : file, PRECIPITATION (a, b) : *
        put PRECIPITATION (a, b)
    end for
end for

for k : 1 .. 7
    for l : 1 .. 5
        for s : 1 .. length (PRECIPITATION (k, l))
            if index (",", PRECIPITATION (k, l) (s)) not= 0 then
                PRECIPITATION (k, l) := PRECIPITATION (k, l) + " "
            end if
        end for
    end for
end for
close : file

The second part, start with the for K, is where i am running into errors. My objective is to index the array to find the commas, and replace them with spaces but i am getting fustrated. Any help is appreciated, as always.

BTW. I have checked the string manipulation tutorials, i cannot seem to apply any of the principles to a 2D array with any success.

-----------------------------------
Cervantes
Mon Feb 27, 2006 9:34 pm

Re: Comma - Delimited - File (CSV) reading issue
-----------------------------------
Ok so for this turing assignment which cervantes is doing with me...
Doing?  Heh.

You read in each line, one by one.  You just do a bit of string manipulation to separate values.

Here's two ideas.
1.  Assign the first element to the line, up to (not inclusive) the comma.  Then chop off the first part of the line (the part up to and inclusive of the comma).  Repeat.
2.  For loop through each character in line, and if it is a comma, add one to a counter variable.  If not, concatenate the character (in line) to the end of your_array (current_row, counter)

-----------------------------------
Da_Big_Ticket
Tue Feb 28, 2006 3:58 pm


-----------------------------------
So something along the lines of

 open : file, "f:/percipitation.csv", get % get information from file
for a : 1 .. 35 % number of pieces of data to store in inp_array (1d)
    exit when eof (file)
    get : file, inp_array (a) : *
    for i : 1 .. length (inp_array (a))
        if inp_array (i) = "," then % Search for commas
            counter += 1
        elsif inp_array (i) not= "," then 
            PRECIPITATION (row, counter) += inp_array (i) % add the characters to precipitation (2d array)
        end if
    end for
    put inp_array (a)
end for


-----------------------------------
Cervantes
Tue Feb 28, 2006 5:05 pm


-----------------------------------
Why are you storing each line from the file in an array?  You don't need it after the data has been read into your 2d array.  So, you can just use a single string variable, called line or some such, and overwrite data in it.  Each time you update it, use it to update your 2D array.

Also, it is highly recommended that you don't use ALL CAPS for variable names.  I don't know if any conventions use ALL CALS for anything.
Caps are generally reserved for Constants and Classes and Modules. 

-----------------------------------
Delos
Tue Feb 28, 2006 8:42 pm


-----------------------------------
I don't know if any conventions use ALL CALS for anything.

Constants?  You ain't never seen constants in all caps?

-----------------------------------
[Gandalf]
Tue Feb 28, 2006 8:46 pm


-----------------------------------
Constants?  You ain't never seen constants in all caps?

Caps are generally reserved for Constants and Classes and Modules.  I don't know if any conventions use ALL CAPS for anything.

*cough*  Re-read his post? :)

I might be mistaken, I think BASIC originally had all variables in full caps as convention.

-----------------------------------
Delos
Tue Feb 28, 2006 9:33 pm


-----------------------------------
*cough* *cough*.  Gandalf, I think you may have a cold.  I just re-read his post and it looks like you're making stuff up!  Don't do that!  It's just mean to put words in other peoples (virtual-)mouths.

-----------------------------------
Cervantes
Tue Feb 28, 2006 9:43 pm


-----------------------------------
Delos: I most certainly have, but I'm not so sure that what I say was convention. 


Caps are generally reserved for Constants and Classes and Modules.

I was referring to CapitalFirstLetter, not ALLLETTERSCAPITALIZED.  I don't know if that's what you got of it, or not.  I'm confused, so I'm going to assume everyone else is, and so I'll try to dispel that.

-----------------------------------
Delos
Tue Feb 28, 2006 9:46 pm


-----------------------------------
On a totally related topic:

/draw -> Confuse -> cast

And Cervantes, reread your post so you know how evil Gandalf is, what with his misquotings!
Sorry DBT, this topic might just get locked soon...

-----------------------------------
[Gandalf]
Tue Feb 28, 2006 9:54 pm


-----------------------------------
A cold?  Why... yes, I do.

Hmm... wait a second!  Abuse of power!  I am now so confused that I forgot what I originally meant.  
*destroys self in an explosion of confused-madness*

Now...  How to say something on-topic...  So...  What's a csv file?

-----------------------------------
Cervantes
Tue Feb 28, 2006 11:03 pm


-----------------------------------
"]So...  What's a csv file?

Comma Separated Value.  Just a bunch of values separated by commas.  If you'd done the [url=http://www.compsci.ca/v2/viewtopic.php?t=10540]Ruby Test, you'd be familiar with them.

-----------------------------------
wtd
Wed Mar 01, 2006 1:30 pm


-----------------------------------
Also, it is highly recommended that you don't use ALL CAPS for variable names.  I don't know if any conventions use ALL CALS for anything.
Caps are generally reserved for Constants and Classes and Modules. 

Indeed,  Eiffel class names are always all-caps.
