From: Rob Biedenharn Date: 2007-07-27T11:24:04+09:00 Subject: Re: How to delete array element and add to previous element On Jul 26, 2007, at 9:59 PM, Al Cholic wrote: > I reposting my last response since there is no edit on this forum. > > Sorry about the syntax. I was just trying to show the array, it > was not > intended to be ruby code. What Im trying to do is read a text file > and > then make sense of the data in that text file and store that info in a > database. I have managed to sort out into the arrays but need to get > the POS2c etc elements into the same array (im guessing thats the best > way to do it). > > For example here is code that im shuffling: > > 49 7 RP13,RP15,RP17,RP19,RP24, 12X2 33XD Wireless, > Independent, 5% > RP32,RP33 > 50 4 RP27,RP28,RP30,RP31 10XC4 3X567 Network, Isolated, 5% > 51 31 R1,R2,R8,R30,R32,R33,R35, 0603,R10K,1% 0603 3%,1/12W > R37,R39,R41,R42,R43,R49, > R50,R51,R52,R58,R68,R69, > R71,R72,R74,R85,R95,R117, > R129,R130,R155,R156,R158, > R160 > > What I've done is split the text up into lines stored all the lines in > an array with text.strip.split("\n") But as you can see the R71, > R129, > the elements in the third column continue on the next line. I need a > way to get them into one place. Because those elements that are on > the > next line are a continuation of the third column element. The > elements > on the line are separated by tabs. I'm guessing that "51\t31\tR1,R2,..." means that this is the 51st "thing" and there are 31 of the Rnn's in the third column. The following lines begin "\t\tR37,R39,..." and "\t\tR50,R51,..." In that case, this fragment might help jump-start you toward a solution: text = gets # or however you're getting the next line data = text.split("\t") # or .split("\t", 6) to limit to 6 parts expected = data[1].to_i # here's where you get 31 data[2] = data[2].split(',').compact # the .compact removes a possible nil from a trailing ',' found = data[2].length while found < expected moretext = gets break unless moretext =~ /\A\t\t/ # \A anchors to start, then 2 TABs more_elements = moretext.split("\t",4)[2].split(',').compact data[2].concat(more_elements) found += more_elements.length end unless found == expected raise "found #{found}, but expected #{expected} on number #{data[0]}" end Yes, it's rough (but so is your problem statment ;-) and you might do better checking for the trailing comma on the line as an added indication that there are more elements to come. Good Luck, -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com