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

What does 'do' statement do in perl?

495


Explain lexical variables.

513


What are some common methods to all handles in perl?

498


Explain returning values from subroutines?

532


Write the program to process a list of numbers.

494






Why to use perl scripting?

519


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 ?

2495


How to dereference a reference?

560


How to merge two arrays in perl?

517


Enlist the advantages of using c over perl?

511


What does redo statement do in perl?

527


Explain the execution of a program in perl.

499


Hi, I am a accountant. I am preparing a balance sheet but because of staff shortage and time pressures I cant complete it on time. There is lot of common data with last years which I need not retype and I can manage by editing last year’s balance sheet ? Is their any software on net where I can do this easily??

1754


Explain the various characteristics of perl.

517


Show the use of sockets for the server and client side of a conversation?

468