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?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

How to close a directory in perl?

504


What are perl strings?

522


Explain the meaning of subroutine?

527


How to access parameters passed to a subroutine in perl?

547


How we can navigate the xml documents?

547






What are the various uses of perl?

535


What does redo statement do in perl?

549


What are perl array functions?

541


How the interpreter is used in Perl?

543


Why -w argument is used with perl programs?

563


what is the difference between java and cgi?

569


How to open a directory in perl?

556


Write a program to download the contents from www.perlinterview.com/answers.php website in Perl.

544


What is automatic error handling in perl?

513


What is perl I used for?

541