What is the difference between for & foreach, exec &
system?
Answers were Sorted based on User's Feedback
Answer / mukesh
for and foreach are alias of each other.But syntax is different.
e.g for(my $i = 0; $i < 3; ++$i){print $i}
e.g foreach my $i (0 .. 2){print $i}
i.e in foreach we can take always only array of elements or
range of elements.But in for we dont have to take the array
or range always.here we can provide condition of the loop
where it is not true for foreach.
Is This Answer Correct ? | 28 Yes | 7 No |
Answer / satish kumar golukonda
for and foreach works alike.
But for uses condition expression, where as foreach works on
ranges, contineous indexing.
Is This Answer Correct ? | 10 Yes | 3 No |
The For and Foreach loop works quite same......but
Foreach loop is best way to access dynamic arrays.If we don't know the size of the array then we can't mention the range for the "FOR LOOP" in this case for loop is best
Is This Answer Correct ? | 5 Yes | 2 No |
Answer / ramesh
Both Perl's exec() function and system() function execute a
system shell command. The big difference is that system()
creates a fork process and waits to see if the command
succeeds or fails - returning a value. exec() does not
return anything, it simply executes the command. Neither of
these commands should be used to capture the output of a
system call. If your goal is to capture output, you should
use the
$result = system(PROGRAM);
exec(PROGRAM);
Is This Answer Correct ? | 12 Yes | 11 No |
Ofcourse the syntax is completely different but the only
difference is
foreach simply gives an easy way to iterate over arrays.
foreach works only on arrays,
and will issue an error when you try to use it on a variable
with a different data type or an uninitialized variable
where as for loop works on the count length or what ever
please look in to php.net site for reference.
Thanks,
Raj
Is This Answer Correct ? | 3 Yes | 2 No |
Answer / ram nivas verma
What is the difference between the 'for' & 'foreach' loops? I know that they can be used interchangeably then what is the purpose of keeping them separate? Can anyone suggest me a good url which can tell the difference between two?
Is This Answer Correct ? | 2 Yes | 2 No |
Answer / mvamsi
Hi Ramesh, You mean to say that we can't capture output of
the program by using exec function. But its working in my PC.
my $ls = exec('ls');
Is This Answer Correct ? | 2 Yes | 7 No |
Answer / mahendra ratnakar
Technically, there's no difference between for and foreach
other than some style issues.
One is an alias of another. You can do things like this
foreach (my $i = 0; $i < 3; ++$i)
{ # normally this is foreach print $i, "n";}
for my $i (0 .. 2)
{ # normally this is for print $i, "n";}
- exec runs the given process, switches to its name and
never returns while system forks off the given process,
waits for it to complete and then returns.
Is This Answer Correct ? | 12 Yes | 18 No |
Create a function that is only available inside the scope where it is defined ?
You want to read command-line arguements with perl. How would you do that?
Which guidelines by Perl modules must be followed?
Define dynamic scoping.
Which operator in perl is used for the concatenation of two strings?
How will you access an element of a perl array?
Explain chomp, chop, cpan, tk.
Is there any way to add two arrays together?
How to implement a stack in Perl?
Why we use CGI?
Differentiate use and require?
How to check the status of airplane mode (enable/disable) in perl for Android mobile?