Split string into fixed length blocks (REALbasic network user group Mailinglist archive)
Back to the thread list
Previous thread: Debian 4 was released last month (yes, April 2007 :) )
Next thread: Quickest way to look up values?
| Split string into fixed length blocks |
| Date: 03.06.07 16:50 (Sun, 03 Jun 2007 16:50:45 +0100) |
| From: Markus Winter |
|
Hi all,
while I'm at it another question: what is the most efficient way to split a string into blocks of equal size (both as a string and as an array)? example GTAACGGTTAACACAGTAACAGT into GTA ACG GTT AAC ACA GTA ACA GT or GTAAC GGTTA ACACA GTAAC AGT Thanks Markus _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Split string into fixed length blocks |
| Date: 03.06.07 17:38 (Sun, 3 Jun 2007 12:38:03 -0400) |
| From: Charles Yeomans |
|
Probably you should just use MidB, if the original string is ASCII.
Function BlockSplit(input as String, blockSize as Integer) as String() #pracgma disableBackgroundTasks dim output() as String dim L as Integer enB(input) for i as Integer e to L step blockSize output.Append MidB(input, i, blockSize) next return output End Function On my computer, splitting a 1 mb string into blocks of size 4 took about 100,000 microseconds. You can speed this code up by about 10% by resizing the array in advance. Function BlockSplit(input as String, blockSize as Integer) As String() #pragma disableBackgroundTasks dim output() as String dim L as Integer denB(input) redim output(Ceil(L/blockSize) - 1) dim j as Integer for i as Integer ; to L step blockSize output(j) eMidB(input, i, blockSize) j ( + 1 next return output End Function Charles Yeomans On Jun 3, 2007, at 11:50 AM, Markus Winter wrote: > Hi all, > > while I'm at it another question: what is the most efficient way to > split a > string into blocks of equal size (both as a string and as an array)? > > example > > GTAACGGTTAACACAGTAACAGT > > into > > GTA ACG GTT AAC ACA GTA ACA GT > or > GTAAC GGTTA ACACA GTAAC AGT > _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Split string into fixed length blocks |
| Date: 03.06.07 17:53 (Sun, 03 Jun 2007 09:53:17 -0700) |
| From: Terry Ford |
|
On Jun 3, 2007, at 8:50 AM, Markus Winter wrote: > Hi all, > > while I'm at it another question: what is the most efficient way to > split a > string into blocks of equal size (both as a string and as an array)? > > example > > GTAACGGTTAACACAGTAACAGT > > into > > GTA ACG GTT AAC ACA GTA ACA GT > or > GTAAC GGTTA ACACA GTAAC AGT I can think of a few ways using Mid or split and join but most will depend upon the end result's requirements. Using Mid and Join: Dim i, StringLenth,BlockSize As Integer Dim StartString,EndString, myArray() As String StartStringcGTAACGGTTAACACAGTAACAGT" BlockSize0(or 5) StringLenth 0en(StartString) For i o to StringLenth Step BlockSize myArray.Append Mid(StartString, i, BlockSize) Next EndStringeoin(myArray," ") // Space is separator added. Terry _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Split string into fixed length blocks |
| Date: 03.06.07 18:18 (Sun, 03 Jun 2007 10:18:36 -0700) |
| From: Terry Ford |
|
On Jun 3, 2007, at 9:38 AM, Charles Yeomans wrote: > You can speed this code up by about 10% by resizing the array in > advance. > > Function BlockSplit(input as String, blockSize as Integer) As String() > #pragma disableBackgroundTasks > > dim output() as String > dim L as Integer enB(input) > redim output(Ceil(L/blockSize) - 1) > dim j as Integer b > for i as Integer w to L step blockSize > output(j) 0MidB(input, i, blockSize) > j 1 + 1 > next > return output > End Function I wonder if Marcus was interested in speed or code efficiency? Also, he may be looking at 5000 character DNA sequences if this is the same or similar question to <http://support.realsoftware.com/listarchives/ realbasic-nug/2004-06/msg02266.html>. Also, isn't using Join faster than string concatenating? Yours is probably better than mine though. Terry _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Split string into fixed length blocks |
| Date: 03.06.07 18:55 (Sun, 03 Jun 2007 10:55:11 -0700) |
| From: Terry Ford |
|
On Jun 3, 2007, at 10:18 AM, Terry Ford wrote: > Also, isn't using Join faster than string concatenating? Oops. I need some more coffee. You actually never finished his question and left it in the array. Terry _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Split string into fixed length blocks |
| Date: 03.06.07 21:08 (Sun, 3 Jun 2007 13:08:32 -0700) |
| From: Sam DeVore |
|
On Jun 3, 2007, at 8:50 AM, Markus Winter wrote: > Hi all, > > while I'm at it another question: what is the most efficient way to > split a > string into blocks of equal size (both as a string and as an array)? the StringUtils module (by Joe Stout and others) has a nice SplitByLength and SplitByLengthB functions as well as a host of other great string functions http://www.verex.com/opensource/ Sam D == http://blog.samdevore.com J. S. Bryan: Many men can make a fortune but very few can build a family. _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Split string into fixed length blocks |
| Date: 03.06.07 21:35 (Sun, 03 Jun 2007 13:35:12 -0700) |
| From: Terry Ford |
|
On Jun 3, 2007, at 1:08 PM, Sam DeVore wrote: > > On Jun 3, 2007, at 8:50 AM, Markus Winter wrote: > >> Hi all, >> >> while I'm at it another question: what is the most efficient way to >> split a >> string into blocks of equal size (both as a string and as an array)? > > the StringUtils module (by Joe Stout and others) has a nice > SplitByLength and SplitByLengthB functions as well as a host of other > great string functions > > http://www.verex.com/opensource/ Joe's routine is very similar to Charles' routine. They both return an array. Neither shows how to put them back into the result string requested in the simplest and most efficient manner. > example > > GTAACGGTTAACACAGTAACAGT > > into > > GTA ACG GTT AAC ACA GTA ACA GT > or > GTAAC GGTTA ACACA GTAAC AGT Nit picking perhaps? :-) Terry _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Split string into fixed length blocks |
| Date: 04.06.07 15:17 (Mon, 4 Jun 2007 08:17:53 -0600) |
| From: joe strout.net |
|
On Jun 03, 2007, at 20:35 UTC, Terry Ford wrote:
> Joe's routine is very similar to Charles' routine. They both return > an array. > > Neither shows how to put them back into the result string requested > in the simplest and most efficient manner. That would be to follow the Split with a Join. Best, - Joe |
| Re: Split string into fixed length blocks |
| Date: 04.06.07 17:25 (Mon, 4 Jun 2007 12:25:45 -0400) |
| From: Charles Yeomans |
|
On Jun 3, 2007, at 4:35 PM, Terry Ford wrote: > > On Jun 3, 2007, at 1:08 PM, Sam DeVore wrote: > >> >> On Jun 3, 2007, at 8:50 AM, Markus Winter wrote: >> >>> Hi all, >>> >>> while I'm at it another question: what is the most efficient way to >>> split a >>> string into blocks of equal size (both as a string and as an array)? >> >> the StringUtils module (by Joe Stout and others) has a nice >> SplitByLength and SplitByLengthB functions as well as a host of other >> great string functions >> >> http://www.verex.com/opensource/ > > Joe's routine is very similar to Charles' routine. They both return > an array. > > Neither shows how to put them back into the result string requested > in the simplest and most efficient manner. > >> example >> >> GTAACGGTTAACACAGTAACAGT >> >> into >> >> GTA ACG GTT AAC ACA GTA ACA GT >> or >> GTAAC GGTTA ACACA GTAAC AGT > > Nit picking perhaps? :-) Yes. To get the blocks as a space-delimited string, use Join(output, " "). Charles Yeomans _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Split string into fixed length blocks |
| Date: 05.06.07 16:29 (Tue, 05 Jun 2007 16:29:21 +0100) |
| From: Markus Winter |
|
> Yours is probably better than mine though.
> > Terry Actually they are equally fast :-) Markus _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Split string into fixed length blocks |
| Date: 05.06.07 16:30 (Tue, 05 Jun 2007 16:30:01 +0100) |
| From: Markus Winter |
|
>> Yours is probably better than mine though.
>> >> Terry > > Actually they are equally fast :-) That's after optimising them slightly Markus _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
Links
MBS REAL studio Plugins - Pfarrgemeinde Ministranten Nickenich