LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   perl (https://www.linuxquestions.org/questions/programming-9/perl-67434/)

andox 06-23-2003 05:28 AM

perl
 
Im trying to do something in perl but it doesn't seem to work..
im trying to run..

#!/usr/bin/perl

@broswer = ("ns", "ie", "opera")
foreach $broswer (@broswer)
{
print "$broswer\n"
}

but for some reason when i compile this i get..
systax error at test1.pl line 5, near "$browswer("
execution of test1.pl aborted due to comilation erros.

can anyone help?

acid_kewpie 06-23-2003 06:04 AM

ok, it's spelt "browser" first off, but change the variable name, you can't have the same name for two variables. you don't actually *need* a variable in the foreach:

Code:

@browser = ("netscape", "rubbish", "opera", "firebird");
foreach (@browser) {
  print "$_\n"
}

you also missed the semi-colon on the first line.

andox 06-23-2003 06:09 AM

i've tried that and it comes out to say..

syntax error at test1.pl line 6, near ") {"

andox 06-23-2003 06:13 AM

another questions.. if u dont use a name for the foreach loop.. will u still be able to create more than one foreach loop with out creating other complications?

andox 06-24-2003 05:57 AM

??

andox 06-24-2003 06:12 AM

could it be the compiler or is there something wrong with the codes?

andox 06-24-2003 06:13 AM

i have perl v5.8.0

Salz 06-24-2003 06:17 AM

Up to your first example - you're simply missing an ; at the end of the assignment. ; may only be left off at the end of a block, but it's a good idea to put them there too, in case you add some commands. So the correct code reads (spellchecked :study: ):

Code:

#!/usr/bin/perl

@browser = ("ns", "ie", "opera");
foreach $browser (@browser)
{
  print "$browser\n";
}

You see, using browser as scalar and array works as if they are two different variables.

andox 06-24-2003 07:13 AM

OOOH.. thank you SOO MUCH

david_ross 06-24-2003 04:38 PM

To answer your other questions:
1) You can nest loops without naming then - you just can't refer to them should you want to end the loop or progress to the next item.
2) Perl is a scripting language nad is interpreted - not compiled.


All times are GMT -5. The time now is 12:52 AM.