What"s wrong with this code?

What"s wrong with this code?

am 04.09.2005 04:53:18 von mmcnurlin

Here is the code i
# p5_4.pl

@array1 = (14, "cheeseburger", 1.23, -7, "toad");
@array2 = @array1;
$count = 1;
while ($count <= 5) {
print("element $count: $array1[count-1] ");
print("$array2[count-1]\n");
$count++;
}


prints this output:
element 1: toad toad
element 2: toad toad
element 3: toad toad
element 4: toad toad
element 5: toad toad


hear is the perl -v:
This is perl, v5.8.7 built for MSWin32-x86-multi-thread
(with 7 registered patches, see perl -V for more detail)

Copyright 1987-2005, Larry Wall

Binary build 813 [148120] provided by ActiveState http://www.ActiveState.com
ActiveState is a division of Sophos.
Built Jun 6 2005 13:36:37

Perl may be copied only under the terms of either the Artistic License or
the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'. If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

_________________________________________ Mitchell McNurlin ICQ#:235649936
Current ICQ status: SMS: (Send an SMS message to my ICQ): +2783142235649936
More ways to contact me: http://wwp.icq.com/235649936
_________________________________________

Re: What"s wrong with this code?

am 04.09.2005 05:03:40 von mmcnurlin

OOPS! Never mind. Next time I will look at the author's samples before
posting.
wrote in message
news:AktSe.32$Sr1.2285@news.uswest.net...
> Here is the code i
> # p5_4.pl
>
> @array1 = (14, "cheeseburger", 1.23, -7, "toad");
> @array2 = @array1;
> $count = 1;
> while ($count <= 5) {
> print("element $count: $array1[count-1] ");
> print("$array2[count-1]\n");
> $count++;
> }
>
>
> prints this output:
> element 1: toad toad
> element 2: toad toad
> element 3: toad toad
> element 4: toad toad
> element 5: toad toad
>
>
> hear is the perl -v:
> This is perl, v5.8.7 built for MSWin32-x86-multi-thread
> (with 7 registered patches, see perl -V for more detail)
>
> Copyright 1987-2005, Larry Wall
>
> Binary build 813 [148120] provided by ActiveState
> http://www.ActiveState.com
> ActiveState is a division of Sophos.
> Built Jun 6 2005 13:36:37
>
> Perl may be copied only under the terms of either the Artistic License or
> the
> GNU General Public License, which may be found in the Perl 5 source kit.
>
> Complete documentation for Perl, including FAQ lists, should be found on
> this system using `man perl' or `perldoc perl'. If you have access to the
> Internet, point your browser at http://www.perl.org/, the Perl Home Page.
>
> _________________________________________ Mitchell McNurlin ICQ#:235649936
> Current ICQ status: SMS: (Send an SMS message to my ICQ):
> +2783142235649936 More ways to contact me: http://wwp.icq.com/235649936
> _________________________________________
>

Re: What"s wrong with this code?

am 04.09.2005 08:56:34 von Ted Bronson

> print("element $count: $array1[count-1] ");
> print("$array2[count-1]\n");

The mistake is you forgot to put the dollar signs in the subscript, so
the program knows that count is a scalar variable.

it should be

print("element $count: $array1[$count-1] ");
print("$array2[$count-1]\n");

that'll fix it right up.
-----
Cpl. Theodore Bronson
lazarus *at* california [dash] resident |dot| com
"An armed society is a polite society.
Manners are good when one may have to
back up his acts with his life." - RAH
-----

Re: What"s wrong with this code?

am 04.09.2005 14:49:34 von Paul Lalli

mmcnurlin@usfamily.net wrote:
> Here is the code i
> # p5_4.pl
>
> @array1 = (14, "cheeseburger", 1.23, -7, "toad");
> @array2 = @array1;
> $count = 1;
> while ($count <= 5) {
> print("element $count: $array1[count-1] ");
> print("$array2[count-1]\n");
> $count++;
> }
>
>
> prints this output:
> element 1: toad toad
> element 2: toad toad
> element 3: toad toad
> element 4: toad toad
> element 5: toad toad

Of course it does. Were you expecting something else? 'count' is a
bareword, which (because you didn't use strict) is treated as a string.
That string, when used with the - operator, is treated as the number
0. 0 - 1 = -1. Every time through the loop.

use strict; would have prevented the use of 'count' as a bareword.
use warnings; would have told you you were using a string in a numeric
context.

Please ask the interpreter for all the help it will give you.

Paul Lalli

Re: What"s wrong with this code?

am 05.09.2005 00:54:21 von mmcnurlin

YEP. I alrady know.
"Ted Bronson" wrote in message
news:ek5lh15g4bknqp547jha66u40nq5nrsosk@4ax.com...
>
>> print("element $count: $array1[count-1] ");
>> print("$array2[count-1]\n");
>
> The mistake is you forgot to put the dollar signs in the subscript, so
> the program knows that count is a scalar variable.
>
> it should be
>
> print("element $count: $array1[$count-1] ");
> print("$array2[$count-1]\n");
>
> that'll fix it right up.
> -----
> Cpl. Theodore Bronson
> lazarus *at* california [dash] resident |dot| com
> "An armed society is a polite society.
> Manners are good when one may have to
> back up his acts with his life." - RAH
> -----
>

Re: What"s wrong with this code?

am 05.09.2005 00:55:33 von mmcnurlin

Sorry I already figured it out.
"Paul Lalli" wrote in message
news:1125838174.153263.194980@f14g2000cwb.googlegroups.com.. .
> mmcnurlin@usfamily.net wrote:
>> Here is the code i
>> # p5_4.pl
>>
>> @array1 = (14, "cheeseburger", 1.23, -7, "toad");
>> @array2 = @array1;
>> $count = 1;
>> while ($count <= 5) {
>> print("element $count: $array1[count-1] ");
>> print("$array2[count-1]\n");
>> $count++;
>> }
>>
>>
>> prints this output:
>> element 1: toad toad
>> element 2: toad toad
>> element 3: toad toad
>> element 4: toad toad
>> element 5: toad toad
>
> Of course it does. Were you expecting something else? 'count' is a
> bareword, which (because you didn't use strict) is treated as a string.
> That string, when used with the - operator, is treated as the number
> 0. 0 - 1 = -1. Every time through the loop.
>
> use strict; would have prevented the use of 'count' as a bareword.
> use warnings; would have told you you were using a string in a numeric
> context.
>
> Please ask the interpreter for all the help it will give you.
>
> Paul Lalli
>
>

Re: What"s wrong with this code?

am 06.09.2005 00:25:57 von Joe Smith

mmcnurlin@usfamily.net wrote:
> Sorry I already figured it out.

Wrong answer.

Paul gave you a piece of advice that is different from all your
other postings on this subject. You should have thanked him
for that, and resolved to always 'use strict; use warnings;'
in your scripts.

-Joe

Re: What"s wrong with this code?

am 08.09.2005 00:05:30 von mmcnurlin

My perl book doesn't specifiy how to use these tools.

"Joe Smith" wrote in message
news:asmdnT4ApcZrWIHeRVn-sA@comcast.com...
> mmcnurlin@usfamily.net wrote:
>> Sorry I already figured it out.
>
> Wrong answer.
>
> Paul gave you a piece of advice that is different from all your
> other postings on this subject. You should have thanked him
> for that, and resolved to always 'use strict; use warnings;'
> in your scripts.
>
> -Joe

Re: What"s wrong with this code?

am 08.09.2005 01:23:36 von Matt Garrish

wrote in message
news:TuJTe.356$YZ1.2482@news.uswest.net...
> "Joe Smith" wrote in message
> news:asmdnT4ApcZrWIHeRVn-sA@comcast.com...
>> mmcnurlin@usfamily.net wrote:
>>> Sorry I already figured it out.
>>
>> Wrong answer.
>>
>> Paul gave you a piece of advice that is different from all your
>> other postings on this subject. You should have thanked him
>> for that, and resolved to always 'use strict; use warnings;'
>> in your scripts.
>>
> My perl book doesn't specifiy how to use these tools.
>

Then your book is garbage and you should throw it away. If you don't know
how/why you should use the warnings and strictures pragmas then you're just
setting yourself up for a world of debugging problems.

Matt