scripting error ?

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

scripting error ?

Mehul Sanghvi-2
OK so I've started scripting and can do the simple

    'Hello' printNl . !

type of script.

I have the following:

     #! /bin/sh
     "exec" "gst" "-f" "$0" "$@"

     'Project Euler:  Problem 1: ' print .

     | sum i limit |
     sum := 0 .
     i := 0 .
     limit := 10 .

     [ i := i + 1.
       i < limit ifTrue: [ ((( i \\ 3) = 0) | ((i \\ 5) = 0)) ifTrue: [ sum :=
sum + i ] ].
     ] repeat.

     !


This will do the print, and then just wait there.  All I want to do is a simple
while-loop type thing where the code loops until counter reaches 1000.  Here's
the equivalent Perl code:


#!/usr/bin/env perl

use integer;

print("\nProblem 1: \n");

$i = 0;
$limit = 1000;
$total_sum = 0;

while ( $i < $limit )
   {
     if ( $i % 3 == 0 )
       {
        $total_sum += $i ;
       }
     elsif ( $i % 5 == 0 )
       {
        $total_sum += $i ;
       }

     $i++ ;
   }

print ("Answer 1: $total_sum\n");




Any suggestions for what I'm doing wrong ?  I'm new to Smalltalk as you can
probably guess by my Smalltalk.


cheers,

     mehul



_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: scripting error ?

Paolo Bonzini-2
Mehul N. Sanghvi wrote:

> OK so I've started scripting and can do the simple
>
>    'Hello' printNl . !
>
> type of script.
>
> I have the following:
>
>     #! /bin/sh
>     "exec" "gst" "-f" "$0" "$@"
>
>     'Project Euler:  Problem 1: ' print .
>
>     | sum i limit |
>     sum := 0 .
>     i := 0 .
>     limit := 10 .
>
>     [ i := i + 1.
>       i < limit ifTrue: [ ((( i \\ 3) = 0) | ((i \\ 5) = 0)) ifTrue: [
> sum := sum + i ] ].
>     ] repeat.
>
>     !
>
>
> This will do the print, and then just wait there.  All I want to do is a
> simple while-loop type thing where the code loops until counter reaches
> 1000.  Here's the equivalent Perl code:

Repeat is an infinite loop.  What you want is

[ i := i + 1. i < limit ] whileTrue: [
  ((( i \\ 3) = 0) | ((i \\ 5) = 0)) ifTrue: [ sum := sum + i ] ].
sum printNl.

or better:

[ i := i + 1. i < limit ] whileTrue: [
  (( i \\ 3) = 0 or: [(i \\ 5) = 0]) ifTrue: [ sum := sum + i ] ].
sum printNl.

or also

1 to: limit do: [ :i |
  (( i \\ 3) = 0 or: [(i \\ 5) = 0]) ifTrue: [ sum := sum + i ] ].
sum printNl.

(Pardon me for any typos, I haven't tried the above).

Paolo


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: scripting error ?

Mehul Sanghvi-2
Paolo Bonzini said the following on 12/09/2008 10:39 AM:

> Mehul N. Sanghvi wrote:
>> OK so I've started scripting and can do the simple
>>
>>    'Hello' printNl . !
>>
>> type of script.
>>
>> I have the following:
>>
>>     #! /bin/sh
>>     "exec" "gst" "-f" "$0" "$@"
>>
>>     'Project Euler:  Problem 1: ' print .
>>
>>     | sum i limit |
>>     sum := 0 .
>>     i := 0 .
>>     limit := 10 .
>>
>>     [ i := i + 1.
>>       i < limit ifTrue: [ ((( i \\ 3) = 0) | ((i \\ 5) = 0)) ifTrue: [
>> sum := sum + i ] ].
>>     ] repeat.
>>
>>     !
>>
>>
>> This will do the print, and then just wait there.  All I want to do is a
>> simple while-loop type thing where the code loops until counter reaches
>> 1000.  Here's the equivalent Perl code:
>
> Repeat is an infinite loop.  What you want is
>

I was under the impression that 'repeat' would repeat the previous block until
the conditions didn't apply.  I guess I'll have to read the 'repeat' doc again.

> [ i := i + 1. i < limit ] whileTrue: [
>   ((( i \\ 3) = 0) | ((i \\ 5) = 0)) ifTrue: [ sum := sum + i ] ].
> sum printNl.
>

This is what I had at one point in time, except that I was doing the counter
increment afterward, which would cause it to just hang and not do anything.

> or better:
>
> [ i := i + 1. i < limit ] whileTrue: [
>   (( i \\ 3) = 0 or: [(i \\ 5) = 0]) ifTrue: [ sum := sum + i ] ].
> sum printNl.
>

I am sure this is a syntax error on my part, but I got the message:

          Object: false error: did not understand #or:ifTrue:

when trying it like above.

> or also
>
> 1 to: limit do: [ :i |
>   (( i \\ 3) = 0 or: [(i \\ 5) = 0]) ifTrue: [ sum := sum + i ] ].
> sum printNl.
>

This was the original I had, but that always gives the wrong answer because
that loop will include the 'limit' as well, whereas what I want is for it to
stop if it equal to limit or greater.

The whileTrue: above worked and did what I wanted it to.  I'll try playing
around the or: syntax to see if I can get that work.  Would be nice to do it
that way.


cheers,

     mehul

p.s. I'm trying to learn Smalltalk by doing the problems at
http://projecteuler.net/



_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: scripting error ?

Paolo Bonzini-2

>          Object: false error: did not understand #or:ifTrue:

Check the parentheses around the #or: part.

> p.s. I'm trying to learn Smalltalk by doing the problems at
> http://projecteuler.net/

Good plan.

Paolo


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Printing Strings

Mehul Sanghvi-2

When I do


     'hello' printNl.

the result I get back is:


bash% ./hello.st
'hello'

bash%


How do I get the "hello" printed without the single quotes around it ?


cheers,

     mehul


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Printing Strings

Paolo Bonzini-2

> How do I get the "hello" printed without the single quotes around it ?

displayNl.  printNl is more programmer-oriented.

Paolo


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Printing Strings

Mehul Sanghvi-2
Paolo Bonzini said the following on 12/09/2008 02:10 PM:
>> How do I get the "hello" printed without the single quotes around it ?
>
> displayNl.  printNl is more programmer-oriented.
>
> Paolo
>


Thanks.  There must be some documentation that tells me how to do \t (tabs) and
such with display/print ?   Any pointers to it ?



cheers,

     mehul



_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Printing Strings

Stephen-71
Hi Mehul,

I've posted a number of GNU Smalltalk scripts at
http://smalltalk.gnu.org/wiki/Examples. And yes, I'm pretty new to
Smalltalk as well... about a year.

Regards
Stephen

Mehul N. Sanghvi wrote:

> Paolo Bonzini said the following on 12/09/2008 02:10 PM:
>>> How do I get the "hello" printed without the single quotes around it ?
>>
>> displayNl.  printNl is more programmer-oriented.
>>
>> Paolo
>>
>
>
> Thanks.  There must be some documentation that tells me how to do \t
> (tabs) and such with display/print ?   Any pointers to it ?
>
>
>
> cheers,
>
>     mehul
>
>
>
> _______________________________________________
> help-smalltalk mailing list
> [hidden email]
> http://lists.gnu.org/mailman/listinfo/help-smalltalk
>
>



_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Printing Strings

Mehul Sanghvi-2
Stephen said the following on 12/09/2008 02:55 PM:
> Hi Mehul,
>
> I've posted a number of GNU Smalltalk scripts at
> http://smalltalk.gnu.org/wiki/Examples. And yes, I'm pretty new to
> Smalltalk as well... about a year.
>
> Regards
> Stephen
>

Nice set of scripts.  I think I overlooked them initially but I might have to
give them a second look.


cheers,

     mehul




_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk