On Sep 1, 5:03 pm, [EMAIL PROTECTED] (Rodrigo Tavares) wrote:
> Hello,
>
> I have a problem with  a counter.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> print "Write a number:";
> my $number= <STDIN>;
>
> my @array;
> my $div = $number * 2 ;
> my $i=0;
>
> while ($div > 0)
>  {
>   $div = $div / 2;
>   $array[$i]=$div;
>   $i++;
>  }
>
>  print "Value i is: $i\n";
>
>   print "$array[0]\n";
>   print "$array[1]\n";
>   print "$array[2]\n";
>   print "$array[3]\n";
>   print "$array[4]\n";
>
> When I run the script:
>
> Write a number:23
> Value i is : 1081
> 23
> 11.5
> 5.75
> 2.875
> 1.4375
>
> Why did script show the value ?

Because you asked it to.
>
> The counter must be six.

Why would you think that?
Instead of the 5 individual print statements of the array elements,
print the entire array and you'll see why it's not 6.

print "$_\n" for @array;
print "Value i is: $i\n";

Then rerun the script after changing:
while ($div > 0)

to this:
while ($div > 1)

>
> Best regards,
>
>       Flickr agora em português. Você clica, todo mundo 
> vê.http://www.flickr.com.br/


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to