For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2005 > Problam in loop









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 Problam in loop
Rita

2005-11-17, 6:56 pm

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

Chris

2005-11-18, 7:55 am

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.

Sponsored Links







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

Copyright 2008 codecomments.com