my @array=('data1','data2');
my @array1=('data1','data2');
my ($i,$k);

$i=7;
$k=7;

while($i){
$array [++$#array] = 'ree';
$i--;
print "@array";
}

while($k){
push(@array1,'ree');
$k--;
print "@array1";
}


Are these two while loop are doing the same functionality ?
What may be the difference?

Answers were Sorted based on User's Feedback



my @array=('data1','data2'); my @array1=('data1','data2'); ..

Answer / neo

Both are same

Is This Answer Correct ?    2 Yes 1 No

my @array=('data1','data2'); my @array1=('data1','data2'); ..

Answer / sugumar

Both are absolutely same in giving the output.
but in first method "$array [++$#array] = 'ree';"
we are preincrementing the array index manually and assigning the latest index to 'ree'; (SLower since we doing it as manual)

Where as in the second method, push is an array function where we need not care about index. it automatically increases the index value for the array. (Faster)

Both give same output as

data1 data2 ree
data1 data2 ree ree
data1 data2 ree ree ree
data1 data2 ree ree ree ree
data1 data2 ree ree ree ree ree
data1 data2 ree ree ree ree ree ree
data1 data2 ree ree ree ree ree ree ree


VALUES FOR K
data1 data2 ree
data1 data2 ree ree
data1 data2 ree ree ree
data1 data2 ree ree ree ree
data1 data2 ree ree ree ree ree
data1 data2 ree ree ree ree ree ree
data1 data2 ree ree ree ree ree ree ree

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More CGI Perl Interview Questions

Explain perl. When do you use perl for programming?

0 Answers  


What can be done for efficient parameter passing in perl?

0 Answers  


How to read a file into a hash array?

0 Answers  


Mention the difference between die and exit in Perl?

0 Answers  


How would you replace a char in string and how do you store the number of replacements?

2 Answers  


What is epoch time in perl?

0 Answers  


How can I implement the function overloading in Perl ? I read about the operator overloading, I do not know how to implement the function overloading. Thanks in advance ?

0 Answers  


Write a program that shows the distinction between child and parent process?

0 Answers  


What are scalar data and scalar variables?

0 Answers  


How to renaming a file in perl programming?

0 Answers  


How to create a directory in perl?

0 Answers  


What are stdin, stdout and stderr?

0 Answers  


Categories