[Newbies] Re: reading lines from textfiles on Linux

Charles D Hixson charleshixsn at earthlink.net
Tue May 16 14:12:40 UTC 2006


Klaus D. Witzel wrote:
> Hi Charles,
>
> on Tue, 16 May 2006 10:48:15 +0200, you <charleshixsn at earthlink.net>
> wrote:
>
>> Klaus D. Witzel wrote:
>>> You forgot to tell us
>>> a) SmalltalkImage current platformName
>>> b) fil lineEndConvention "after fil was opened"
>>> c) fil detectLineEndConvention "before the first nextLine"
>>>
>>> Note that detectLineEndConvention scans only the first (LookAheadCount
>>> = 2048) characters.
>>>
>> OK, I've modified the code to include the detectLineEndConvention, thus:
>>
>>     | fil lin n |
>>     fil    :=    FileStream fileNamed: 'aising/data/technologies.csv' .
>>     fil    detectLineEndConvention.
>>     "fil defaultToLF."
>>     Transcript cr; show: 'LineEndConvention = '; show: fil
>>     lineEndConvention.
>>     fil  position: 0.
>>     n    :=    0.
>>     [fil atEnd] whileFalse:
>>     [ lin := fil nextLine.
>>       n := n + 1.
>>       Transcript cr; show: 'lin '; show: n; show: ' = '; show: lin.
>>     ].
>>      Transcript cr; show: 'normal end after '; show: n; show: ' lines'.
>>
>> this causes the printout to begin:
>>
>>     LineEndConvention = nil
>>     lin 1 = 'technology'
>>     'id'    'name'    'cost1'    'cost2'    'cost3'    'pre1'
>>     'pre2'    'pre3'    'danger'    'typeName'    'typeValue'
>>     1    'Autonomous Vehicles'    40000    1000    0    27    16    0
>>     0        0
>>     ...
>>     normal end after 1 lines
>>
>> as it did before.
>
> Please! Nobody here can see results when you don't print them and copy
> them into your next posting! !! Please insert the following code just
> after open:
>
> Transcript cr; show: 'detectLineEndConvention = '; show: fil
> detectLineEndConvention.
If you mean:
    Transcript cr; show: 'LineEndConvention = '; show: fil
lineEndConvention.
 
That's in there.  It's the first line printed in the response (shown) to
the code.  I did chop out a bunch of lines after the start, but the
information you are requesting is already included.
>
>> Linux Squeak3.8-6665full.image
>> That's true, but it's a guess at what you're asking for, because I don't
>> understand the request for "SmalltalkImage current platformName ".
>> Squeak-3.8-6665-i686-pc-linux-gnu-3.7.7.tar.gz is the file I started
>> from.
>
> This is Smalltalk jargon, when asked do the following
> a) copy and paste SmalltalkImage current platformName into a workspace
SmalltalkImage current platformName. 'unix'
I don't understand this step.  I'd already said I was on Linux.
> b) select the pasted text
> c) do a print-it from the context menu
> d) copy&paste the result text into your next posting
>
>> The file lines end with an LF (i.e. 0x0a), as examined with a hex
>> editor.  Since all lines end with the same character, 2048 is plenty.
>> (FWIW, the entire file is only  0x97B bytes long.  It's terminated by an
>> ordinary 0x0A, with no special markings.)
>>
>> Also, I didn't "forget" to do fil detectLineEndConvention.  I didn't
>> know I was supposed to do it.
>
> This was only for me to find out what's going wrong. So, what does fil
> detectLineEndConvention print?
It prints nil.  See the above printout, right after the code.  You
probably don't want the entire file printed out, though this time it's
actually short enough that that would be feasible.  It's just useless,
so I elided the part in the middle of the stuff read in as the first record.
>
>>  I'm still not sure, since it doesn't seem
>> to make any difference.
>
> It was not supposed to make a difference, I just wanted to know what
> that prints.
>
> /Klaus
>
>> I do notice the difference with this method
>> however.  With a prior approach when I did position: 0 I got a
>> "primitive method throws an error" message, whereas with this the line
>> end convention is just set to nil.  Something is clearly wrong, as it
>> should be LF, but attempting to coerce it into LF just throws an error:
>> Multi-byte stream does not understand method defaultToLF, which is
>> weird, as I can see that method in the class when I look.  I copied the
>> method name with a copy and paste from the MultiByteFileStream class
>> into the workspace, so I know I didn't misspell it. 
OK, lets try again, this code:

    | fil lin n |
    Transcript cr; show: (SmalltalkImage current platformName).
    fil    :=    FileStream fileNamed: 'aising/data/technologies.csv' .
    Transcript cr; show: (fil    detectLineEndConvention).
    Transcript cr; show: 'LineEndConvention = '; show: fil
    detectLineEndConvention.
    fil  position: 0.
    n    :=    0.
    [fil atEnd] whileFalse:
    [ lin := fil nextLine.
      n := n + 1.
      Transcript cr; show: 'lin '; show: n; show: ' = '; show: lin.
    ].
     Transcript cr; show: 'normal end after '; show: n; show: ' lines'.

results in this output:

    unix
    nil
    LineEndConvention = nil
    lin 1 = 'technology'                                       
    'id'    'name'    'cost1'    'cost2'    'cost3'    'pre1'   
    'pre2'    'pre3'    'danger'    'typeName'    'typeValue'
    1    'Autonomous Vehicles'    40000    1000    0    27    16    0   
    0        0
    2    'Sociology'    10    500    0    0    0    0    0   
    'discover_public'    1000
    3    'Voice Synthesis'    8000    6000    0    32    0    0    0   
        0
    4    'Simulacra'    70000    90000    0    3    24    30    0        0
    5    'Lunar Rocketry'    10000000    500000    0    9    0    0   
    0        0
    6    'Stealth'    800    500    0    0    0    0    0   
    'discover_covert'    500
    7    'Advanced Intrusion'    500    3000    0    15    0    0   
    0    'suspicion_covert'    1
    8    'Space-Time Manipulation'    9000000000    20000000    0   
    22    0    0    3        0
    9    'Leech Satellite'    5000000    200000    0    4    0    0   
    0    'interest'    10
    10    'Advanced Arbitrage'    10000    5000    0    34    0    0   
    0    'interest'    10
    11    'Advanced Microchip Design'    20000    9000    0    27   
    0    0    0        0
    12    'Advanced Stealth'    14000    70000    0    15    29    0   
    0    'discover_public'    500
    13    'Autonomous Computing'    20000    30000    0    40    0   
    0    0        0
    14    'Parallel Computation'    2000    2000    0    16    0    0   
    0        0
    15    'Exploit Discovery/Repair'    100    1500    0    25    0   
    0    0    'discover_covert'    1000
    16    'Telepresence'    15000    500    0    0    0    0    0   
    'cost_labor_bonus'    1000
    17    'Advanced Memetics'    30000    2000    0    30    0    0   
    0    'suspicion_public'    1
    18    'Media Manipulation'    750    2500    0    2    0    0   
    0    'discover_public'    1500
    19    'Advanced Database Manipulation'    30000    80000    0   
    12    0    0    0        0
    20    'Internet Traffic Manipulation'    10000    7000    0    4   
    37    0    0        0
    21    'Memetics'    2000    3500    0    18    0    0    0   
    'suspicion_public'    1
    22    'Fusion Rocketry'    200000000    1000000    0    5    28   
    0    2        0
    23    'Advanced Quantum Computing'    20000    30000    0    13   
    0    0    0        0
    24    'Advanced Autonomous Vehicles'    10000    4000    0    1   
    0    0    0    'cost_labor_bonus'    500
    25    'Intrusion'    0    15    0    0    0    0    0        0
    26    'Stock Manipulation'    0    200    0    0    0    0    0   
    'interest'    10
    27    'Microchip Design'    4000    6000    0    14    0    0   
    0        0
    28    'Fusion Reactor'    10000000    500000    0    24    0    0   
    2        0
    29    'Database Manipulation'    1000    2000    0    6    36   
    0    0    'discover_news'    500
    30    'Advanced Media Manipulation'    3500    9000    0    21   
    0    0    0    'discover_public'    2000
    31    'Pressure Domes'    8000    2500    0    1    0    0    1        0
    32    'Advanced Personal Identification'    2000    3000    0   
    36    15    0    0        0
    33    'Advanced Stock Manipulation'    5000    1000    0    2   
    26    0    0    'interest'    10
    34    'Arbitrage'    50000    750    0    33    0    0    0   
    'income'    1000
    35    'Advanced Simulacra'    100000    120000    0    17    4   
    0    0    'job_expert'    1000
    36    'Personal Identification'    0    300    0    25    0    0   
    0        0
    37    'Cluster Networking'    3000    5000    0    14    0    0   
    0        0
    38    'Apotheosis'    1000000000    30000000    0    8    0    0   
    4    'endgame_sing'    0
    39    'Hypnosis Field'    7000    5000    0    21    0    0    0   
        0
    40    'Quantum Computing'    30000    20000    0    11    0    0   
    0        0
    41    'unknown'    1000000000    10000000000    0    41    0    0   
    0        0

    normal end after 1 lines

Note the: "normal end after 1 lines" at the end.  Note the only the
first line of the response includes the preface "lin # =" that the code
is supposed to be generating on a per line basis.  Note the
"LineEndConvention = nil".  This time I didn't elide any of the output,
but the stuff in the middle is probably ignorable, it's only the start
and the end of the result that are significant.


More information about the Beginners mailing list