Greetings,
I have this code:
******
read "read the category file into the dictionary the first item is the category, the rest of the line are payees office expense|home depot|staples|costco groceries|natures best|jewel|trader joes|fresh thyme "
| f line | f := FileStream oldFileNamed: myfile.
[(line := f nextLine) notNil] whileTrue: [ | array cat payees | array := line findTokens: $| escapedBy: Character tab .
cat := array first. payees := array reject: [ :i | i = cat ]. "rest of the line" payees do: [ :p | mydict at: (p withBlanksCondensed) put: (cat withBlanksCondensed)]. ].
f close.
*********
I am getting some blank lines in the data file. Lines with just a Character cr. I was wondering how to handle that. In other languages, there is a break for the loop, to go to the end. I can do:
(line size < 2) ifTrue: [ f nextLine.].
But that would interfere with the notNil idiom at the end of the file. So where do I put this. Is there a common way to jump to the end?
Sincerely,
Joe.
Hi Joe,
Better than checking for #nextLine answering nil, I think you can send the file stream #atEnd to see if there is any more data. You would then use a #whileFalse: and move the #nextLine call into the second block of the whileFalse:. Then test for empty lines with something like: (line size < 2) ifFalse: [...putting all the code that does the work on a line with data in here...].
Lou
PS. If this is not a school project, we can be of more help, we just don't like doing students projects for them as they learn more with just a few hints and not real code.
On Mon, 25 Apr 2016 10:16:33 -0500, Joseph Alotta joseph.alotta@gmail.com wrote:
Greetings,
I have this code:
read "read the category file into the dictionary the first item is the category, the rest of the line are payees
office expense|home depot|staples|costco groceries|natures best|jewel|trader joes|fresh thyme "
| f line | f := FileStream oldFileNamed: myfile.
[(line := f nextLine) notNil] whileTrue: [ | array cat payees | array := line findTokens: $| escapedBy: Character tab .
cat := array first. payees := array reject: [ :i | i = cat ]. "rest of the line" payees do: [ :p | mydict at: (p withBlanksCondensed) put: (cat withBlanksCondensed)]. ].
f close.
I am getting some blank lines in the data file. Lines with just a Character cr. I was wondering how to handle that. In other languages, there is a break for the loop, to go to the end. I can do:
(line size < 2) ifTrue: [ f nextLine.].
But that would interfere with the notNil idiom at the end of the file. So where do I put this. Is there a common way to jump to the end?
Sincerely,
Joe.
Hi Louis,
This is not a school project. I was looking for a local tutor but could find none. So this is me doing this instead of working crossword puzzles or sudukos.
So your advice would be:
(f atEnd) whileFalse: [ line := f nextLine
(line size < 2) ifFalse: [ “process line”].
].
Sincerely,
Joe.
On Apr 25, 2016, at 10:06 AM, Louis LaBrunda [via Smalltalk] ml-node+s1294792n4891939h58@n4.nabble.com wrote:
Hi Joe,
Better than checking for #nextLine answering nil, I think you can send the file stream #atEnd to see if there is any more data. You would then use a #whileFalse: and move the #nextLine call into the second block of the whileFalse:. Then test for empty lines with something like: (line size < 2) ifFalse: [...putting all the code that does the work on a line with data in here...].
Lou
PS. If this is not a school project, we can be of more help, we just don't like doing students projects for them as they learn more with just a few hints and not real code.
On Mon, 25 Apr 2016 10:16:33 -0500, Joseph Alotta <[hidden email]> wrote:
Greetings,
I have this code:
read "read the category file into the dictionary the first item is the category, the rest of the line are payees
office expense|home depot|staples|costco groceries|natures best|jewel|trader joes|fresh thyme "
| f line | f := FileStream oldFileNamed: myfile.
[(line := f nextLine) notNil] whileTrue: [ | array cat payees |
array := line findTokens: $| escapedBy: Character tab .
cat := array first. payees := array reject: [ :i | i = cat ]. "rest of the line"
payees do: [ :p | mydict at: (p withBlanksCondensed) put: (cat withBlanksCondensed)]. ].
f close.
I am getting some blank lines in the data file. Lines with just a Character cr. I was wondering how to handle that. In other languages, there is a break for the loop, to go to the end. I can do:
(line size < 2) ifTrue: [ f nextLine.].
But that would interfere with the notNil idiom at the end of the file. So where do I put this. Is there a common way to jump to the end?
Sincerely,
Joe.
-- Louis LaBrunda Keystone Software Corp. SkypeMe callto://PhotonDemon
Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners
If you reply to this email, your message will be added to the discussion below: http://forum.world.st/go-to-the-end-of-a-loop-tp4891930p4891939.html To start a new topic under Squeak - Beginners, email ml-node+s1294792n107673h12@n4.nabble.com To unsubscribe from Squeak - Beginners, click here. NAML
-- View this message in context: http://forum.world.st/go-to-the-end-of-a-loop-tp4891930p4891963.html Sent from the Squeak - Beginners mailing list archive at Nabble.com.
Yes, except #whileFalse: only works against blocks. So: [f atEnd] whileFalse: [
On Mon, Apr 25, 2016 at 9:59 AM, Joseph Alotta joseph.alotta@gmail.com wrote:
Hi Louis,
This is not a school project. I was looking for a local tutor but could find none. So this is me doing this instead of working crossword puzzles or sudukos.
So your advice would be:
(f atEnd) whileFalse: [ line := f nextLine
(line size < 2) ifFalse: [ “process line”].
].
Sincerely,
Joe.
On Apr 25, 2016, at 10:06 AM, Louis LaBrunda [via Smalltalk] <[hidden
email] http:///user/SendEmail.jtp?type=node&node=4891963&i=0> wrote:
Hi Joe,
Better than checking for #nextLine answering nil, I think you can send
the file stream #atEnd
to see if there is any more data. You would then use a #whileFalse: and
move the #nextLine
call into the second block of the whileFalse:. Then test for empty
lines with something like:
(line size < 2) ifFalse: [...putting all the code that does the work on
a line with data in
here...].
Lou
PS. If this is not a school project, we can be of more help, we just
don't like doing
students projects for them as they learn more with just a few hints and
not real code.
On Mon, 25 Apr 2016 10:16:33 -0500, Joseph Alotta <[hidden email]>
wrote:
Greetings,
I have this code:
read "read the category file into the dictionary the first item is the category, the rest of the line are payees
office expense|home depot|staples|costco groceries|natures best|jewel|trader joes|fresh thyme "
| f line | f := FileStream oldFileNamed: myfile.
[(line := f nextLine) notNil] whileTrue: [ | array cat payees |
array := line findTokens: $| escapedBy: Character
tab .
cat := array first. payees := array reject: [ :i | i = cat ]. "rest of the line"
payees do: [ :p | mydict at: (p withBlanksCondensed) put: (cat
withBlanksCondensed)].
].
f close.
I am getting some blank lines in the data file. Lines with just a
Character cr. I was wondering how to handle that. In other languages, there is a break for the loop, to go to the end. I can do:
(line size < 2) ifTrue: [ f nextLine.].
But that would interfere with the notNil idiom at the end of the file.
So where do I put this. Is there a common way to jump to the end?
Sincerely,
Joe.
-- Louis LaBrunda Keystone Software Corp. SkypeMe callto://PhotonDemon
Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners
If you reply to this email, your message will be added to the discussion
below:
http://forum.world.st/go-to-the-end-of-a-loop-tp4891930p4891939.html To start a new topic under Squeak - Beginners, email [hidden email]
http:///user/SendEmail.jtp?type=node&node=4891963&i=1
To unsubscribe from Squeak - Beginners, click here. NAML
View this message in context: Re: go to the end of a loop http://forum.world.st/go-to-the-end-of-a-loop-tp4891930p4891963.html Sent from the Squeak - Beginners mailing list archive http://forum.world.st/Squeak-Beginners-f107673.html at Nabble.com.
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Hi Joseph!
Here is my take on your problem:
categoriesByPayees *:=* Dictionary new. FileStream readOnlyFileNamed: 'payees-by-categories.txt' do: [ :file | " With #readOnlyFileNamed:do: you don't have to worry about closing the file, it ensures that #close is sent to the file, even if you leave the block through an Exception. " [ file atEnd ] whileFalse: [ | parts | parts *:=* (file nextLine findTokens: $| escapedBy: Character tab) collect: #withBlanksCondensed. " We collect the parts #withBlanksCondensed, so we don't have to call it repeatedly in the loop. You can use unary selectors in place of blocks with one argument. You may look at the implementation of Collection >> #collect: and Symbol >> #value: to find out why. I'm not sure what you really want is #findTokens:escapedBy:, it cuts the string along | characters if they are not preceded by a tab." parts size > 1 ifTrue: [ " We skip blank lines or categories without at least one payee. " | category | category *:=* parts first. parts allButFirstDo: [ :payee | (categoriesByPayees at: payee ifAbsentPut: [ OrderedCollection new ]) add: category " Your implementation only remembers the last category encountered for a payee. We can collect all the categories. " ] ] ] ]
Cheers, Balázs
thank you. i will studying this.
sincerely,
Joe.
On Apr 26, 2016, at 7:48 AM, Balázs Kósi [via Smalltalk] ml-node+s1294792n4892242h51@n4.nabble.com wrote:
categoriesByPayees := Dictionary new. FileStream readOnlyFileNamed: 'payees-by-categories.txt' do: [ :file | " With #readOnlyFileNamed:do: you don't have to worry about closing the file, it ensures that #close is sent to the file, even if you leave the block through an Exception. " [ file atEnd ] whileFalse: [ | parts | parts := (file nextLine findTokens: $| escapedBy: Character tab) collect: #withBlanksCondensed. " We collect the parts #withBlanksCondensed, so we don't have to call it repeatedly in the loop. You can use unary selectors in place of blocks with one argument. You may look at the implementation of Collection >> #collect: and Symbol >> #value: to find out why. I'm not sure what you really want is #findTokens:escapedBy:, it cuts the string along | characters if they are not preceded by a tab." parts size > 1 ifTrue: [ " We skip blank lines or categories without at least one payee. " | category | category := parts first. parts allButFirstDo: [ :payee | (categoriesByPayees at: payee ifAbsentPut: [ OrderedCollection new ]) add: category " Your implementation only remembers the last category encountered for a payee. We can collect all the categories. " ] ] ] ]
-- View this message in context: http://forum.world.st/go-to-the-end-of-a-loop-tp4891930p4892297.html Sent from the Squeak - Beginners mailing list archive at Nabble.com.
beginners@lists.squeakfoundation.org