|
|
|
| Hi Members,
i started to learning perl and i have a problam may be it's so simple
but i am confuse
what is the foreach loop
is it should be on an array or i can use on scalar too
thanks
| |
| usenet@DavidFilmer.com 2005-11-17, 6:56 pm |
| Rita wrote:
> what is the foreach loop
perldoc perlsyn
It is a looping control structure (it's the same as a "for" loop -
there is no difference). You would apply it against an array.
For example:
foreach my $foo (1..10) {
print "$foo\n";
}
woud print:
1
2
3
....
10
| |
|
| Rita wrote:
> Hi Members,
> i started to learning perl and i have a problam may be it's so simple
> but i am confuse
> what is the foreach loop
> is it should be on an array or i can use on scalar too
> thanks
>
You can use a foreach loop on a scalar if you want (try it),
but why would you want to? if you want to 'work' with the
scalar then just use it as is:
print $scalar;
$scalar += 5;
etc...
| |
| Eden Cardim 2005-11-18, 7:55 am |
| > You can use a foreach loop on a scalar if you want (try it),
> but why would you want to? if you want to 'work' with the
> scalar then just use it as is:
>
> print $scalar;
> $scalar += 5;
> etc...
There are plenty of 'perlish' ways to use a for loop with a single
scalar effectively. For example, perlsyn suggests using for's aliasing
with a single scalar in order to create a convenient switch mechanism.
|
|
|
|