Smalltalk (Squeak) - String parsing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Smalltalk (Squeak) - String parsing

dyfroedd

I'm learning Smalltalk programming as I think its way ahead as an OOP programming tool. I would appreciate feedback regarding my coding of the following problem. I have a sequence of letters representing bases in a DNA and want to change the T to a U in the string. I have done this in two ways. The first uses the CopyReplaceAll message to the string object which changes every occurrence of 'T' to a 'U':

string := 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'.
string copyReplaceAll: 'T' with: 'U'. 

Mrs second method uses a brute force method of using a for loop equivalent and boolean conditions to test whether a 'T' is identified. I learned that I have to copy the string to an Array object, then iterate over that object and copy the elements to an empty array. Ideally I would like the original string object to be mutable. Am I going at this the wrong way? Here's my code:

string := 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'.
strCopy := string asArray.
empty := Array new: strCopy size.
cntr := 1.

strCopy do: [:char | 
     (char = $T) ifTrue: [empty at:cntr put: $U ]
    ifFalse: [empty at:cntr put: char].
    cntr := cntr + 1
    ].

I appreciate your guidance. 

Best regards