For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2005 > Problem with Foreach in If Statement...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with Foreach in If Statement...
Hakim Singhji

2005-06-04, 8:55 am

-----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-----
John W. Krahn

2005-06-04, 8:55 am

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
Frank Lee

2005-06-04, 3:55 pm

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com