Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Perl (http://www.programmingforums.org/forum21.html)
-   -   small error.. (http://www.programmingforums.org/showthread.php?t=13344)

teishu Jun 13th, 2007 4:11 PM

small error..
 
hi, just learning perl can someone tell me whats wrong with this:


:

print "The months of the year are: \n";
@months = ("January \n", "Febuary \n", "March \n", "April \n", "and so on... \n")
for $i (1, 2, 3, 4, 5) {
        print "@months \n";
        }


i get this error:

:

teishu@debian:~/prl$ perl months.pl
syntax error at months.pl line 5, near "$i ("
Execution of months.pl aborted due to compilation errors.


thanks

DaWei Jun 13th, 2007 4:48 PM

No error that trashes your program is a small error. The forum gets lebenty-jillion posts that claim failures are a bug. Bugs are relatively common, but they don't hold a candle, percentage-wise, to the mistakes that are programmer generated. Instead of asking "What's wrong with this", would you care to explain the difference between what you expect and what you get?

If not, we'll have to presume what you want versus what you get. At zero dollars per hour, that's not very profitable. Regardless of our altruistic propensities, that's not very satisfying.

Your move.

Infinite Recursion Jun 13th, 2007 9:17 PM

You are missing a ; after your array list. It should run then, but you probably won't get what you expect.

snipertomcat Jun 14th, 2007 12:24 AM

Exactly what do you want the program to do?

DaWei Jun 14th, 2007 12:52 AM

Every language and every application has fuck-ups. Ninety-nine percent of them are upstream, between the user's fingertips and the user's purported brain. Would you care to expand upon your problem, in some sort of meaningful detail?

Infinite Recursion Jun 14th, 2007 2:03 AM

Lets take a random stab in the dark and assume he wants to print out the contents of the month array accessing one element at a time. :| If this is not what you want, elaborate.

:

#!/usr/bin/perl

print "The months of the year are: \n";
@months = ("January", "Febuary", "March", "April", "and so on...");
for ($i = 0; $i < 5; $i++)
{
        print "$months[$i]\n";
}


On a side note, why even include a newline in the values of the array? This is more of a formatting option than it is part of the data... so imho, its best left for the print statement to handle.

On another side note, why even care how many elements the array has when printing it, if you are going to print them all anyway... do this:

:

#!/usr/bin/perl

print "The months of the year are: \n";
@months = ("January \n", "Febuary \n", "March \n", "April \n", "and so on... \n");

foreach $myMonth (@months)
{
        print "$myMonth\n";
}


teishu Jun 15th, 2007 7:08 PM

Quote:

Originally Posted by Infinite Recursion (Post 129148)
Lets take a random stab in the dark and assume he wants to print out the contents of the month array accessing one element at a time. :| If this is not what you want, elaborate.

:

#!/usr/bin/perl

print "The months of the year are: \n";
@months = ("January", "Febuary", "March", "April", "and so on...");
for ($i = 0; $i < 5; $i++)
{
        print "$months[$i]\n";
}


On a side note, why even include a newline in the values of the array? This is more of a formatting option than it is part of the data... so imho, its best left for the print statement to handle.

On another side note, why even care how many elements the array has when printing it, if you are going to print them all anyway... do this:

:

#!/usr/bin/perl

print "The months of the year are: \n";
@months = ("January \n", "Febuary \n", "March \n", "April \n", "and so on... \n");

foreach $myMonth (@months)
{
        print "$myMonth\n";
}


yeh, thats what i was trying to do... im just learning the basic syntax at the moment...

Infinite Recursion Jun 17th, 2007 9:47 AM

Perl is a good language... it gets more cryptic as you go. :) Glad it worked out for ya.

mackenga Jul 10th, 2007 5:54 PM

Yeah, good luck with Perl - it's actually quite a gentle learning curve and very rewarding, but there's an enormous amount of it to learn. I've been programming in Perl for years now and still feel like I've just scratched the surface - but that's not to say it hasn't already been incredibly useful to me. Nice choice. When you meet regular expressions, don't run for the hills: read slowly, understand them a bit at a time and they're amazing.


All times are GMT -5. The time now is 4:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC