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

List the files in current directory sorted by size ?

666


How to do comment in perl?

739


What is a chop() function in perl?

766


How to merge two arrays in perl?

729


What is goto statement in perl?

698


How to connect with sqlserver from perl and how to display database table info?

730


Explain lists ?

691


How do you set environment variables in perl?

727


what are the three groups involved in information sharing?

651


What is the use of '>>' in perl?

712


What is the use of -w, -t and strict in Perl?

750


Explain arrays in perl.

707


How to read multi lines from a file in perl?

683


How will you declare a variable in perl?

726


Mention the difference between die and exit in Perl?

837