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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

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

RedTigerFish
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.

<http://forum.world.st/file/t371379/errormessage.png

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.
Reply | Threaded
Open this post in threaded view
|

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

Benoit St-Jean
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_Benito
IRC: 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 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.
--
_______________________________________________
Beginners mailing list

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

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

Ben Coman
In reply to this post by RedTigerFish
On 16 November 2017 at 16:19, RedTigerFish <[hidden email]> 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.

<http://forum.world.st/file/t371379/errormessage.png>

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 -ben
Feed 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
Reply | Threaded
Open this post in threaded view
|

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

RedTigerFish
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.
Reply | Threaded
Open this post in threaded view
|

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

RedTigerFish
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.
Reply | Threaded
Open this post in threaded view
|

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

Yoshiki Ohshima-3
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
Reply | Threaded
Open this post in threaded view
|

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

Benoit St-Jean
In reply to this post by RedTigerFish
I usually prefer simplicity... ;)  If you'd tell me you have to scan a million numbers, I would have picked another solution!  ;)


-----------------
Benoît St-Jean
Yahoo! Messenger: bstjean
Twitter: @BenLeChialeux
Pinterest: benoitstjean
Instagram: Chef_Benito
IRC: lamneth
Blogue: endormitoire.wordpress.com
"A standpoint is an intellectual horizon of radius zero".  (A. Einstein)


On Thursday, November 16, 2017, 9:42:36 PM EST, RedTigerFish <[hidden email]> wrote:


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

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

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

Ben Coman
In reply to this post by RedTigerFish
Okay, it looks like you did the work. So I'll offer an alternative.
Move the 'a' assignment to the top of the loop, immediately preceded by the bounds check,
with the beneficial side-effect that the loop exits earlier.

myArray := #(2 32 44 67 89 111 123).
i := 1.
n := 1.
nMax := myArray size.
[ (i <=130) and: (n<=nMax) ] whileTrue: 
[    a := myArray at: n.
    (i = a) ifTrue: 
    [   Transcript show: i; cr.
        n := n +1.
    ].
].


I think "array includes: i"  is nice. But I guess my way is faster. Correct

yes. But is #includes: fast enough?  You are trading *one* line of code for *ten*.  
That makes  #includes:  faster write, so you stay more in-the-zone of your application domain,
and forever-after makes it faster to understand every time your read your code.

You should avoid premature optimisation, and only use your implementation once benchmarks show its a problem.

cheers -ben

On 17 November 2017 at 10:57, 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


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners