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

What does the qq{ } operator do?

0 Answers  


Explain socket programming in perl?

0 Answers  


Why should I use the -w argument with my Perl programs?

0 Answers  


What $! In perl?

0 Answers  


How many loop control keywords are there in perl?

0 Answers  






How do I send e-mail from a Perl/CGI program on a Unix system?

0 Answers  


How to implement a stack in Perl?

0 Answers  


What elements of the Perl language could you use to structure your code to allow for maximum re-use and maximum readability?

1 Answers  


How can memory be managed in Perl?

0 Answers  


What are the different ways to run cgi?

0 Answers  


package MYCALC; use Exporter; our @EXPORT = (); our @ISA = qw(Exporter); our @EXPORT_OK = qw(addition multi); our %EXPORT_TAGS = (DEFAULT => [qw(&addition)],Both => [qw(&addition & +multi)]); sub addition { return $_[0] + $_[1]; } sub multi { return $_[0] * $_[1]; } 1; Program: use strict; use warnings; my @list = qw (2 2); use Module qw(:DEFAULT); print addition(@list),"\n"; Above coding is my module MYCALC and the program which using this module, I have not exported any function using @EXPORT, but I have used the DEFAULT in %EXPORT_TAGS with the function addition, when I call this function from the main it says the error as,

0 Answers  


how to install a package in perl ????

2 Answers  


Categories