# "Print specific integers" Error message, how to improve and what causes that ?

8 messages
Open this post in threaded view
|

## "Print specific integers" Error message, how to improve and what causes that ?

 This post was updated on . Hello, *what I want is:* From integers 1 to 130, I want to print some specific integers already given in an array. They are: 2 32 44 67 89 111 123 which are stored in small-to-big order. *Here's my codes:* |a n myArray| myArray := #(2 32 44 67 89 111 123). n := 1. a := myArray at: n. 1 to: 130 do: [:i|         i = a         ifTrue: [                 Transcript show: i; cr.                 n := n + 1.                 a := myArray at: n.                 ].         ]. The output is very good except for an Error Message.   By my current level, I have no idea why that Error Message appears. Q1: Why  Error Message appears ? Q2: How can I improve that? ----- Dig, dig where you are, Down below's well. Let those that walk in darkness shout, Down below's hell. -- Sent from: http://forum.world.st/Squeak-Beginners-f107673.html_______________________________________________ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners Dig, dig where you are, Down below's well. Let those that walk in darkness shout, Down below's hell.
Open this post in threaded view
|

## Re: "Print specific integers" Error message, how to improve ?

 Q1) Because you increment n when you hit 123, so the index is now 8 but your array only has 7 elements. Q2) You could do it in a zillion way, the simplest one is like:| myArray|myArray := #(2 32 44 67 89 111 123).1 to: 130 do: [:i | (myArray includes: i) ifTrue: [ Transcript show: i; cr ]].----------------- Benoît St-Jean Yahoo! Messenger: bstjean Twitter: @BenLeChialeux Pinterest: benoitstjean Instagram: Chef_BenitoIRC: lamneth Blogue: endormitoire.wordpress.com "A standpoint is an intellectual horizon of radius zero".  (A. Einstein) On Thursday, November 16, 2017, 3:19:49 AM EST, RedTigerFish <[hidden email]> wrote: Hello, *what I want is:*From integers 1 to 130, I want to print some specific integers already givenin an array.They are: 2 32 44 67 89 111 123 which are stored in small-to-big order.*Here's my codes:*|a n myArray|myArray := #(2 32 44 67 89 111 123).n := 1.a := myArray at: n. 1 to: 130 do: [:i|    i = a    ifTrue: [        Transcript show: i; cr.        n := n + 1.         a := myArray at: n.        ].        ].The output is very good except for an Error Message. By my current level, I have no idea why that Error Message appears. Q1: Why  Error Message appears ? Q2: How can I improve that?-----Dig, dig where you are,Down below's well.Let those that walk in darkness shout, Down below's hell.--_______________________________________________Beginners mailing list _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: "Print specific integers" Error message, how to improve ?

 In reply to this post by RedTigerFish On 16 November 2017 at 16:19, RedTigerFish wrote:Hello, *what I want is:* From integers 1 to 130, I want to print some specific integers already given in an array. They are: 2 32 44 67 89 111 123 which are stored in small-to-big order. *Here's my codes:* |a n myArray| myArray := #(2 32 44 67 89 111 123). n := 1. a := myArray at: n. 1 to: 130 do: [:i|         i = a         ifTrue: [                 Transcript show: i; cr.                 n := n + 1.                 a := myArray at: n.                 ].         ]. The output is very good except for an Error Message. By my current level, I have no idea why that Error Message appears. Q1: Why  Error Message appears ?Rather than give a direct answer, I think you'll gain the most if shown the path how to solve it yourself. The key question is... What is the value of 'n' when the error occurs? Click on the line Undefined>>DoIt line to open a debugger at that point and observe the instance variables.Lets call that 'problemN'.  How does this compare to the number of elements in your array?Then try this...problemN := "whatever it is".n := 1.a := myArray at: n.1 to: 130 do: [:i|        i = a        ifTrue: [                Transcript show: i; cr.                n := n + 1.                (n+1 = problemN) ifTrue: [ self halt].                a := myArray at: n.                ].        ].and from where it halts, practice with StepOver, StepThrough and StepInto to observe the code execution.  Q2: How can I improve that?Consider what is the value of 'n' when 123 is assigned to 'a', and then what happens when next 'i=a' is true.cheers -benFeed a man a fish, and he eats for a day.Teach a man to fish, and he eats for life. _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: "Print specific integers" Error message, how to improve ?

 In reply to this post by Benoit St-Jean To: Benoit St-Jean I think* array includes: i * is nice. But I guess my way is faster. Correct ? ----- Dig, dig where you are, Down below's well. Let those that walk in darkness shout, Down below's hell. -- Sent from: http://forum.world.st/Squeak-Beginners-f107673.html_______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners Dig, dig where you are, Down below's well. Let those that walk in darkness shout, Down below's hell.
Open this post in threaded view
|

## Re: "Print specific integers" Error message, how to improve and what causes that ?

 In reply to this post by RedTigerFish *Update:* I found another way to solve this issue: |myArray| myArray := #(2 32 44 67 89 111 123). n := 1. a := myArray at: n. 1 to: 130 do: [:i|     i = a           ifTrue: [         Transcript show: i; cr.         n := n +1.         a := myArray at: n.         n = 7         ifTrue: [n := n - 1].         ].       ]. *Looks ugly though* ----- Dig, dig where you are, Down below's well. Let those that walk in darkness shout, Down below's hell. -- Sent from: http://forum.world.st/Squeak-Beginners-f107673.html_______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners Dig, dig where you are, Down below's well. Let those that walk in darkness shout, Down below's hell.
Open this post in threaded view
|

## Re: "Print specific integers" Error message, how to improve and what causes that ?

 This is a part of something bigger problem/project, right?  Otherwise, you get the same results from: | myArray | myArray := #(2 32 44 67 89 111 123). myArray do: [:a | Transcript show: a; cr]. or even, #(2 32 44 67 89 111 123) do: [:a | Transcript show: a; cr]. (Why 130?) On Thu, Nov 16, 2017 at 6:57 PM, RedTigerFish <[hidden email]> wrote: > *Update:* > > I found another way to solve this issue: > > |myArray| > > myArray := #(2 32 44 67 89 111 123). > > n := 1. > a := myArray at: n. > > 1 to: 130 do: [:i| >     i = a >     ifTrue: [ >         Transcript show: i; cr. >         n := n +1. >         a := myArray at: n. >         n = 7 >         ifTrue: [n := n - 1]. >         ]. >     ]. > > > *Looks ugly though* > > > > ----- > Dig, dig where you are, > Down below's well. > Let those that walk in darkness shout, > Down below's hell. > -- > Sent from: http://forum.world.st/Squeak-Beginners-f107673.html> _______________________________________________ > Beginners mailing list > [hidden email] > http://lists.squeakfoundation.org/mailman/listinfo/beginners-- -- Yoshiki _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners