Hi John,

On Monday 03 May 2010 17:42:16 John W. Krahn wrote:
> Samuel Williams wrote:
> > Dear Friends,
> 
> Hello,
> 
> > It would also be great if someone could rewrite the Perl source code
> > 
> > example so that it is as close as possible to the C implementation:
> >     http://programming.dojo.net.nz/languages/c/index
> > 
> > /* Include the standard input / output functions */
> > #include <stdio.h>
> > 
> > int main()
> > {
> > 
> >     /* Initialize the array of doors to 0 (closed) */
> >     char is_open[100] = {0};
> 
> Why are you creating an array of 101 elements when you only use 100?
> Your comment says you initialize the array but you only initialize the
> first element.

Actually, in C:

1. A << type_t myarray[100]; >> will initialise an array that can be indexed 
from 0 to 99 - 100 elements in total. Likewish myarray[10] will be indexed 
from 0 to 9, etc.

2. The {0} is a shortened initialisation to allocate all the elements as 0. 

> 
> >     int pass, door;
> >     
> >     /* Process the doors */
> >     for (pass = 0; pass < 100; ++pass)
> >     
> >         for (door = pass; door < 100; door += pass+1)
> >         
> >             is_open[door] = !is_open[door];
> >     
> >     /* Print out the results */
> >     for (door = 0; door < 100; ++door)
> >     
> >         printf("Door #%d is %s.\n", door+1, (is_open[door] ? "open." :
> >         "closed."));
> >     
> >     return 0;
> > 
> > }
> 
> sub main()
> {

1. Why did you put everything in a main fuction? Perl is not C.

2. Why the "()" after the main? You shouldn't specify prototypes.

>      # Initialize the array of doors to 0 (closed)
>      $is_open[100] = 0;

1. Why didn't you declare @is_open? You should have used strict and warnings.

2. This will initialise the element of index 100 (actually the 101st element) 
to 0 and all the elements before it (assuming they did not exist beforehand) 
to undef. You probably want << @is_open = ((0) x 100) >> instead.

> 
>      # Process the doors
>      for ($pass = 0; $pass < 100; ++$pass){

1. PBP recommends against doing C-style for loops.

2. you should declare pass again.

3. You should have some space before the "{" (I start it on a newline and have 
my reasons for that but I accept the other style).

>          for ($door = $pass; $door < 100; $door += $pass+1){
>              $is_open[$door] = !$is_open[$door];}}

Ahmmm.. why didn't you put the two "}" in separate lines? Perl is not Lisp.

> 
>      # Print out the results
>      for ($door = 0; $door < 100; ++$door){
>          printf("Door #%d is %s.\n", $door+1, ($is_open[$door] ? "open."
> 
> : "closed."));}

Just a note - the C program has a bug where the strings had a trailing "." 
which was also included in the printf's formatting string. It should not be 
reproduced in the Perl program.

Regards,

        Shlomi Fish

> 
>      return 0;
> }
> 
> 
> 
> 
> John

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Rethinking CPAN - http://shlom.in/rethinking-cpan

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to