Home > Archive > PerlTk > December 2005 > foreach instruction
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 |
foreach instruction
|
|
| xyoavx 2005-12-09, 7:15 pm |
| Hi,
given the following piece of a program:
foreach (@array)
{
*********
*********
}
I know that sometimes, the lack of the parenheses around @array,
displays an error. I would like to understand the rule. I mean, when
they must be and when not.
Thanks in advance.
Regards,
xyoavx
| |
| John W. Krahn 2005-12-09, 10:09 pm |
| xyoavx wrote:
> Hi,
> given the following piece of a program:
> foreach (@array)
> {
> *********
> *********
> }
> I know that sometimes, the lack of the parenheses around @array,
> displays an error. I would like to understand the rule. I mean, when
> they must be and when not.
The parentheses are required by the for/foreach/while/until loop syntax so
they must ALWAYS be there.
perldoc perlsyn
John
--
use Perl;
program
fulfillment
| |
|
|
"John W. Krahn" <someone@example.com> wrote in message
news:I%qmf.144047$S4.23697@edtnps84...
> xyoavx wrote:
>
> The parentheses are required by the for/foreach/while/until loop syntax so
> they must ALWAYS be there.
>
> perldoc perlsyn
>
>
> John
> --
> use Perl;
> program
> fulfillment
I was pretty sure that was the case as well.
So, original poster, could you post an example where it has not been?
barry
| |
| xyoavx 2005-12-10, 8:12 am |
| Hello,
Here is an example, including its output:
#!perl -w
#file 1
print "something\n",foreach 1..5,"\n";
print "nothing\n" foreach 1..5;# pay attention the comma, to the left
of foreach isn't needed.
print foreach 1..3,"\n","hello\n";
@x=1..3;
print "no parentheses\n",foreach @x ,"\n";
C:\perl-pro>p 1
something
something
something
something
something
something
nothing
nothing
nothing
nothing
nothing
123
hello
no parentheses
no parentheses
no parentheses
no parentheses
| |
| Ala Qumsieh 2005-12-10, 7:12 pm |
| xyoavx wrote:
> Hello,
> Here is an example, including its output:
>
> #!perl -w
> #file 1
> print "something\n",foreach 1..5,"\n";
> print "nothing\n" foreach 1..5;# pay attention the comma, to the left
> of foreach isn't needed.
> print foreach 1..3,"\n","hello\n";
> @x=1..3;
> print "no parentheses\n",foreach @x ,"\n";
There are two valid syntaxes for 'foreach':
foreach VAR (@LIST) { BODY }
BODY foreach @LIST;
The first one requires the parentheses, the second doesn't. It's just
the way the syntax was defined.
--Ala
| |
| John W. Krahn 2005-12-10, 7:12 pm |
| xyoavx wrote:
> Hello,
> Here is an example, including its output:
>
> #!perl -w
> #file 1
> print "something\n",foreach 1..5,"\n";
> print "nothing\n" foreach 1..5;# pay attention the comma, to the left
> of foreach isn't needed.
> print foreach 1..3,"\n","hello\n";
> @x=1..3;
> print "no parentheses\n",foreach @x ,"\n";
>
> C:\perl-pro>p 1
> something
[snip output]
What you are using is called a Statement Modifier (lookup the "Statement
Modifiers" section of perlsyn.) The foreach statement modifier does not
require parentheses around the list the same way that print, map, grep and
sort, etc. do not require parentheses around their lists. The foreach loop is
different because it requires the parentheses and the code block and will work
with the redo, next, last and continue keywords.
John
--
use Perl;
program
fulfillment
| |
| John W. Krahn 2005-12-10, 7:12 pm |
| Ala Qumsieh wrote:
> xyoavx wrote:
>
> There are two valid syntaxes for 'foreach':
>
> foreach VAR (@LIST) { BODY }
> BODY foreach @LIST;
>
> The first one requires the parentheses, the second doesn't. It's just
> the way the syntax was defined.
I think you meant:
foreach VAR (LIST) { BODY }
STATEMENT foreach LIST;
The '@' sigil implies that an array is required. :-)
John
--
use Perl;
program
fulfillment
| |
| xyoavx 2005-12-10, 7:12 pm |
| Hi,
Thanks for your promt answer.
Regards,
xyoavx
|
|
|
|
|