LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-23-2003, 05:28 AM   #1
andox
Member
 
Registered: May 2003
Location: Mars
Distribution: Red Hat Linux 9
Posts: 100

Rep: Reputation: 15
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?
 
Old 06-23-2003, 06:04 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 06-23-2003, 06:09 AM   #3
andox
Member
 
Registered: May 2003
Location: Mars
Distribution: Red Hat Linux 9
Posts: 100

Original Poster
Rep: Reputation: 15
i've tried that and it comes out to say..

syntax error at test1.pl line 6, near ") {"
 
Old 06-23-2003, 06:13 AM   #4
andox
Member
 
Registered: May 2003
Location: Mars
Distribution: Red Hat Linux 9
Posts: 100

Original Poster
Rep: Reputation: 15
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?
 
Old 06-24-2003, 05:57 AM   #5
andox
Member
 
Registered: May 2003
Location: Mars
Distribution: Red Hat Linux 9
Posts: 100

Original Poster
Rep: Reputation: 15
??
 
Old 06-24-2003, 06:12 AM   #6
andox
Member
 
Registered: May 2003
Location: Mars
Distribution: Red Hat Linux 9
Posts: 100

Original Poster
Rep: Reputation: 15
could it be the compiler or is there something wrong with the codes?
 
Old 06-24-2003, 06:13 AM   #7
andox
Member
 
Registered: May 2003
Location: Mars
Distribution: Red Hat Linux 9
Posts: 100

Original Poster
Rep: Reputation: 15
i have perl v5.8.0
 
Old 06-24-2003, 06:17 AM   #8
Salz
LQ Newbie
 
Registered: Jun 2003
Distribution: Debian
Posts: 23

Rep: Reputation: 15
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 ):

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.
 
Old 06-24-2003, 07:13 AM   #9
andox
Member
 
Registered: May 2003
Location: Mars
Distribution: Red Hat Linux 9
Posts: 100

Original Poster
Rep: Reputation: 15
OOOH.. thank you SOO MUCH
 
Old 06-24-2003, 04:38 PM   #10
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 05:37 PM
cgi perl : I cant get perl to append my html file... the_y_man Programming 3 03-22-2004 05:07 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM
can i set ~/public_html/perl as a perl directory for apache? doublefailure Linux - Networking 1 07-09-2002 04:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration