Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Problem with Foreach in If Statement...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi All,

This is my first post at beginners. I am working with Learning Perl
(Llama!) and I like to tweak some of the problems and create more
creative solutions to some things. I cannot figure out why this is not
working. Can anyone point out an error in my code.

#! Perl

@names = qw( fred barney betty wilma dino );
print "Enter some numbers 1 to 5, one per line, then press ctrl Z:\n";
chomp (@numbers = <STDIN> );
~  if (@numbers > 5) {
print "Error: choose numbers less then 6!\n";
~  } else {
foreach (@numbers); {
print "$names[ $_ - 1 ]\n";
}
Best,
- --
Hakim Singhji
New York University
hzs202@nyu.edu
http://i5.nyu.edu/~hzs202
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCoTsjs2AZ6g3dZu8RAtAdAJ44jsNG/TUOiP4kDyuZWN3tWIrrFQCdFB+V
WKsRYfcrHasOTOB4lhrlMOg=
=K40v
-----END PGP SIGNATURE-----

Report this thread to moderator Post Follow-up to this message
Old Post
Hakim Singhji
06-04-05 01:55 PM


Re: Problem with Foreach in If Statement...
Hakim Singhji wrote:
> Hi All,

Hello,

> This is my first post at beginners. I am working with Learning Perl
> (Llama!) and I like to tweak some of the problems and create more
> creative solutions to some things. I cannot figure out why this is not
> working. Can anyone point out an error in my code.
>
> #! Perl
>
> @names = qw( fred barney betty wilma dino );
> print "Enter some numbers 1 to 5, one per line, then press ctrl Z:\n";
> chomp (@numbers = <STDIN> );
> ~  if (@numbers > 5) {
^
^
>       print "Error: choose numbers less then 6!\n";
> ~  } else {
^
^
>       foreach (@numbers); {
^
^
>           print "$names[ $_ - 1 ]\n";
>       }

Remove the characters indicated by ^ and it should work.


John
--
use Perl;
program
fulfillment

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
06-04-05 01:55 PM


Re: Problem with Foreach in If Statement...
please try this one:

#tested in linux
#!/usr/bin/perl -w
use strict;

my @names =3D qw( fred barney betty wilma dino );
print "Enter some numbers 1 to 5, one per line, then press ctrl D:\n";
chomp (my @numbers =3D <STDIN> );
foreach (@numbers){
if ($_ < 6){
print "$names[$_ - 1]\n";
}else{
=09print "Error: choose numbers less then 6!\n";
=09}
}




On 6/4/05, Hakim Singhji <hzs202@gmail.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>=20
> Hi All,
>=20
> This is my first post at beginners. I am working with Learning Perl
> (Llama!) and I like to tweak some of the problems and create more
> creative solutions to some things. I cannot figure out why this is not
> working. Can anyone point out an error in my code.
>=20
> #! Perl
>=20
> @names =3D qw( fred barney betty wilma dino );
> print "Enter some numbers 1 to 5, one per line, then press ctrl Z:\n";
=20
ctrl + D should be used
> chomp (@numbers =3D <STDIN> );
> ~  if (@numbers > 5) {
@number > 5 means the number of element in array @number=20
is large than 5.
it does not mean the element is large than 5.

>           print "Error: choose numbers less then 6!\n";
> ~  } else {
>           foreach (@numbers); {
~~~ no ; here=20
>                   print "$names[ $_ - 1 ]\n";
>           }
need another } here to comparable with "if (@numbers > 5) {"
> Best,
> - --
> Hakim Singhji
> New York University
> hzs202@nyu.edu
> http://i5.nyu.edu/~hzs202
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.5 (MingW32)
> Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
>=20
> iD8DBQFCoTsjs2AZ6g3dZu8RAtAdAJ44jsNG/TUOiP4kDyuZWN3tWIrrFQCdFB+V
> WKsRYfcrHasOTOB4lhrlMOg=3D
> =3DK40v
> -----END PGP SIGNATURE-----
>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>=20
>=20
>=20


--=20
Do not guess who  I am.  I am not Bush in BlackHouse

Report this thread to moderator Post Follow-up to this message
Old Post
Frank Lee
06-04-05 08:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:47 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.