Home > Archive > PERL Programming > September 2005 > What's wrong with this code?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
What's wrong with this code?
|
|
|
| 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
________________________________________
_
| |
|
| OOPS! Never mind. Next time I will look at the author's samples before
posting.
<mmcnurlin@usfamily.net> 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
> ________________________________________
_
>
| |
| Ted Bronson 2005-09-04, 3:55 am |
|
> 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
-----
| |
| Paul Lalli 2005-09-04, 7:55 am |
| 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
| |
|
| YEP. I alrady know.
"Ted Bronson" <moc.tnediser-ainrofilac@surazal> wrote in message
news:ek5lh15g4bknqp547jha66u40nq5nrsosk@
4ax.com...
>
>
> 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
> -----
>
| |
|
| Sorry I already figured it out.
"Paul Lalli" <mritty@gmail.com> wrote in message
news:1125838174.153263.194980@f14g2000cwb.googlegroups.com...
> mmcnurlin@usfamily.net wrote:
>
> 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
>
>
| |
| Joe Smith 2005-09-05, 7:09 pm |
| 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
| |
|
| My perl book doesn't specifiy how to use these tools.
"Joe Smith" <joe@inwap.com> wrote in message
news:asmdnT4ApcZrWIHeRVn-sA@comcast.com...
> mmcnurlin@usfamily.net wrote:
>
> 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
| |
| Matt Garrish 2005-09-07, 6:56 pm |
|
<mmcnurlin@usfamily.net> wrote in message
news:TuJTe.356$YZ1.2482@news.uswest.net...
> "Joe Smith" <joe@inwap.com> wrote in message
> news:asmdnT4ApcZrWIHeRVn-sA@comcast.com...
> 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
|
|
|
|
|