Search code examples
loopscollectionsnestedsmalltalkpharo

Elements from collection get truncated when printing - Smalltalk


I am extremely new to Smalltalk, so please excuse me if I am implementing this solution incorrectly.

I'm reading a table from a txt file that looks like this:

1 3 5
2 4 7
55 30 50

I am reading the file through an readStream as follows:

inStream := inFile readStream.

where inFile is the address of the input file.

inStream gets used to build the table with this method:

readInput: inStream
        
        | line |
        
        rows := OrderedCollection new.
        [ (line := inStream nextLine) notNil ] whileTrue: [ rows add: line.
                                                            Transcript 
                                                                        show: 'read line:';
                                                                        show: line;
                                                                        cr.
                                                          ].

Finally, I'm printing specific columns with this method:

printCols: columns
    "comment stating purpose of instance-side method"
    "scope: class-variables  &  instance-variables"
    
    | columnsCollection |
    
    columnsCollection := columns findTokens: ','.

    rows do: [ :currentRow |
                            columnsCollection do: [ :each |
                                                            Transcript 
                                                                    show: (currentRow at: (each asInteger) );
                                                                    show: ' '.                                                                                          
                                                  ].
                            Transcript cr.
              ].

where columns is a comma separated list of columns I'm interested in printing passed in as a string.

I'm not sure what's wrong in my printCols function, but the output I'm seeing always removes the second number of a given item. For example, when printing column 1, I'll get:

1
2
5

any help is greatly appreciated!


Solution

  • As Leandro Caniglia mentions in the comments of the post, the problem is that currentRow is a String, while I was expecting it to be a collection, and therefore, in the columnsCollection loop, I'm accessing a single character instead of an element from the collection.

    The solution was to change how I'm reading in the rows to make sure they are taken as a collection of collections instead of a collection of Strings.

    readInput: inStream
    
            | line |
            
            rows := OrderedCollection new.
            [ (line := inStream nextLine) notNil ] whileTrue: [ |row|
                                                                    row := OrderedCollection new.
                                                                    row := line findTokens: ' '.
                                                                    rows add: row.
                                                              ].
    

    Thanks for the help!