Welcome to Dream.In.Code
Become an Expert!

Join 149,515 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,343 people online right now. Registration is fast and FREE... Join Now!




A possible bug in Perl for loop?

 
Reply to this topicStart new topic

A possible bug in Perl for loop?

dhfeldman
21 May, 2007 - 03:29 PM
Post #1

New D.I.C Head
*

Joined: 16 May, 2007
Posts: 4


My Contributions
Hi all,
I'm new to Perl and can't figure this one out. I downloaded what I believe to be the current stable version of ActivePerl for Windows (version 5.8.x, I think; how do I find out what version I have? The download file is called ActivePerl-5.8.8.820-MSWin32-x86-274739.zip) I also downloaded OpenPerlIDE, since I wanted a Windows IDE- I have no particular reason for choosing this one except that it is free, and because it was apparently written in Delphi, which I am familiar with). After some experimentation and consulting of manual pages, and posting a question to this form, I've gotten some simple things to work. Every attempt I've made to use a for loop has produced wierd results, such as the peice of code following. When I rewrite the code using a while loop, everything looks okay. Have i stumbled onto a bug in ActivePerl or in the OpenPerlIDE, or am I making some newbie mistake??

[code]
# testforloop.pl
for ($i=1,$i<=10,$i++) {
print ('loop number: ',$i,"\n");
}

[result output]
loop number: 2
loop number: 2
loop number: 2

# strange, wouldn't you say?
# or try this one

# examine representation of numerical values
for ($i=1,$i<=10,$i++) {
$result1=sprintf("%d","$i");
$result2=sprintf("%e","$i");
$result3=sprintf("%f","$1");
print ($result1," ",$result2," ",$result3,"\n");
}
print ("\n");
# strange results!
# now try a while loop- this seems to work
$i=1;
while ($i<=10) {
$result1=sprintf("%d","$i");
$result2=sprintf("%e","$i");
$result3=sprintf("%f","$1");
print ("$i: ",$result1," ",$result2," ",$result3,"\n");
$i++;
}

[result output]
2 2.000000e+000 0.000000
2 2.000000e+000 0.000000
2 2.000000e+000 0.000000

1: 1 1.000000e+000 0.000000
2: 2 2.000000e+000 0.000000
3: 3 3.000000e+000 0.000000
4: 4 4.000000e+000 0.000000
5: 5 5.000000e+000 0.000000
6: 6 6.000000e+000 0.000000
7: 7 7.000000e+000 0.000000
8: 8 8.000000e+000 0.000000
9: 9 9.000000e+000 0.000000
10: 10 1.000000e+001 0.000000

[\code]

Thanks for the help.


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: A Possible Bug In Perl For Loop?
21 May, 2007 - 04:44 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Perl syntax uses the semi-colon as a terminator:
CODE

for ($i=1,$i<=10,$i++)

should be
CODE

for ($i=1;$i<=10;$i++)


User is online!Profile CardPM
+Quote Post

dhfeldman
RE: A Possible Bug In Perl For Loop?
21 May, 2007 - 05:41 PM
Post #3

New D.I.C Head
*

Joined: 16 May, 2007
Posts: 4


My Contributions
Thanks. Figures it would be something dumb like that. I'm a novice at Perl after all. It strikes me odd that the compiler didn't object to my use of the commas. Anyone have some insight into why that is so?
User is offlineProfile CardPM
+Quote Post

KevinADC
RE: A Possible Bug In Perl For Loop?
21 May, 2007 - 08:00 PM
Post #4

D.I.C Head
Group Icon

Joined: 23 Jan, 2007
Posts: 238



Thanked: 6 times
Dream Kudos: 50
My Contributions
QUOTE(dhfeldman @ 21 May, 2007 - 06:41 PM) *

Thanks. Figures it would be something dumb like that. I'm a novice at Perl after all. It strikes me odd that the compiler didn't object to my use of the commas. Anyone have some insight into why that is so?


Because it is valid syntax:

for ($i=1,$i<=10,$i++) {
print ('loop number: ',$i,"\n");
}

outputs:

loop number: 2
loop number: 2
loop number: 2

akin to:

for ($i,$i,$i) {
print ('loop number: ',++$i,"\n");
}

of course the output is different but it shows that the syntax is valid. You can use an easier construct though:

for $i (1..10) {
print "$i\n";
}
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:00PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month