Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Comma - Delimited - File (CSV) reading issue
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Da_Big_Ticket




PostPosted: Mon Feb 27, 2006 8:58 pm   Post subject: 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:

code:
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.
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Mon Feb 27, 2006 9:34 pm   Post subject: Re: Comma - Delimited - File (CSV) reading issue

Da_Big_Ticket wrote:
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




PostPosted: Tue Feb 28, 2006 3:58 pm   Post subject: (No subject)

So something along the lines of

code:
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




PostPosted: Tue Feb 28, 2006 5:05 pm   Post subject: (No subject)

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




PostPosted: Tue Feb 28, 2006 8:42 pm   Post subject: (No subject)

Cervantes wrote:
I don't know if any conventions use ALL CALS for anything.


Constants? You ain't never seen constants in all caps?
[Gandalf]




PostPosted: Tue Feb 28, 2006 8:46 pm   Post subject: (No subject)

Delos wrote:
Constants? You ain't never seen constants in all caps?


Cervantes wrote:
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? Smile

I might be mistaken, I think BASIC originally had all variables in full caps as convention.
Delos




PostPosted: Tue Feb 28, 2006 9:33 pm   Post subject: (No subject)

*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




PostPosted: Tue Feb 28, 2006 9:43 pm   Post subject: (No subject)

Delos: I most certainly have, but I'm not so sure that what I say was convention.

[Gandalf]: When I said
Cervantes wrote:

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.
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Tue Feb 28, 2006 9:46 pm   Post subject: (No subject)

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]




PostPosted: Tue Feb 28, 2006 9:54 pm   Post subject: (No subject)

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




PostPosted: Tue Feb 28, 2006 11:03 pm   Post subject: (No subject)

[Gandalf] wrote:
So... What's a csv file?


Comma Separated Value. Just a bunch of values separated by commas. If you'd done the Ruby Test, you'd be familiar with them.
wtd




PostPosted: Wed Mar 01, 2006 1:30 pm   Post subject: (No subject)

Cervantes wrote:
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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: