We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Loading a 2d array from string
Page Index Toggle Pages: 1
Loading a 2d array from string (Read 575 times)
Loading a 2d array from string
Mar 12th, 2010, 10:21am
 
Hello to all,

With the help from yesterday I was able to clean up my strings as I needed but now I have hit another snag.

What I have is a string that lists all of the winning lottery numbers since it's inception. I am trying to play around with this data. I have successfully removed all of the text and extra whitespace from the file and am left with this type of format:

02 24 10     7     16     24     39     49     53

First three numbers are the date, followed by six lottery numbers. Occasionally throughout the string I also have this:

10     2010     1     12

which is the date the file was generated and the page number (eg. Jan 1, 2010 page 1 of 12)

This list is about 2000 lines long. I am trying to take each row and load it into an array. In my head I picture this:

[0]   [1]   [2]        [3]        [4]        [5]        [6]        [7]        [8]
02    24    10         7           16        24         39        49          53   [0]
??     ??    ??         ?            ??        ??         ??         ??          ??    [1]

All of the rows will have 8 indexes aside from the ones with the page and date. Not sure how I am going to handle those. My code is messy and very beginner I know, bet hey, I just getting started.

I was thinking that I could load the string inside of a nested loop to a 2d array. I was running into problems with trying to find a way to go over the string line by line and load it into the array. I tried using the split() function but every time processing came to a halt. I can only attribute this to the fact that I am shoving quite a bit of data through that function and it chokes. Correct me if I'm wrong.

Using the splitTokens() I was able to get even closer, for every row it would index 0 - 8 (or 0 - 3 on the date/page rows) so I think that I am getting close. I also believe that I should convert the data from strings to int, not sure why I'm thinking that but it just makes sense.

Ultimately, I was looking to parse the array and find the greatest occurrence of numbers. To see which number has been used the most. Sorry for the long post, but I know that more info is better than less. Loading this into a 2d array has me stumped. Any recommendations on this or comments on my code would be appreciated.  

Thanks

Code:
void setup() {
 String[] fileRaw = loadStrings("lottery") ;  // Load file
 int numLines = fileRaw.length ; // Find total number of lines
 clean(fileRaw) ; // Pass to clean
}

void clean(String[] lotteryRaw) {
 //for (int i = 0; i < lotteryRaw.length; i++) { // Commented out
 for (int i = 1; i < 30; i++) {             // for testing
   String remWhitespace = lotteryRaw[i].replaceAll("[\\W]", " ") ;
   String remTextLines = remWhitespace.replaceAll("[a-zA-Z]", "") ;
   String cleanTrim = remTextLines.trim() ; // This is ugly I think.  
   loadArray(cleanTrim);  
 }
}  

void loadArray(String cleanTrim) {
 String[] q = splitTokens(cleanTrim, " ");
 println(q) ;  
// Print out the value of q, which is a nice list of the rows,
// indexed 0 - 8 and 0 - 4, repeating over and over for
// each row.
 
}
Re: Loading a 2d array from string
Reply #1 - Mar 12th, 2010, 10:25am
 
as far as i understand you dont need the page information right? just remove it frmo your file and you dont need to take care of it.
It is always about bringing the data in the right format before parsing it. You dont have to do all by programming. If it is easier, to search and replace 12 lines, do that...
Page Index Toggle Pages: 1