1

I'm trying to go though a array and add characters from that array to another object. The problem is I keep getting a error "Instances of character are not indexable". However when I run tag := tag,char outside of the do block then it works.

|data startTag tag|.
data := '123456778'
startTag := false.
tag := ''.
data asArray do: [:char |
     tag := tag,char] 
MikeC
  • 255
  • 4
  • 16

1 Answers1

3

The , is defined as

Collection>>, aCollection
^self copy addAll: aCollection; yourself

so that tries to operate on your single character as if it were a collection. That explains the error.

For larger collections you do not want to build up using , because of the copy that happens each time. Therefore use the streaming protocol:

|data tag|
data := '123456778'.
tag := String streamContents: [:s |
    data do: [ :char |
    s nextPut: char]]

Also take a look at Collection>>do:separatedBy: to add separators between your data.

[edit] Ah, ok, that's something like

|data tag tags state|
data := '<html>bla 12 <h1/></html>'.
state := #outside.
tags := OrderedCollection new.
tag := ''.
data do: [ :char |
    state = #outside ifTrue: [
        char = $< ifTrue: [ 
            state := #inside.
            tag := '' ]]
    ifFalse:  [ 
         char = $> ifTrue: [ 
            state := #outside.
            tags add: tag] 
        ifFalse: [ tag := tag, (char asString)]]].
tags

"an OrderedCollection('html' 'h1/' '/html')"
Stephan Eggermont
  • 15,847
  • 1
  • 38
  • 65
  • Thanks, but I'm not sure if this will work for what I need. What i'm actually trying to accomplish is reading the html of a web page and extracting all the start tags from the html. So when I hit a '<' I want to start concatenating the strings until I hit '>' which is when I add that string to a collection and start again. – MikeC Feb 25 '16 at 20:52
  • 1
    Are you sure that you want to do that by hand? There *are* tools for that... (e.g. in Pharo we have Soup, a port of Python's BeautifulSoup: http://smalltalkhub.com/#!/~PharoExtras/Soup). – Max Leske Feb 29 '16 at 11:16