Tuesday, February 16, 2010

Perl interview questions Part #6

1) Explain the difference between the following in Perl:
$array[3] vs $array->[3]

Answer:

$array[3] is the 4th element of the array "array"
$array->[3] is the hard reference to a (possible anonymous) array. It's the 4th element of the referenced array.


2) How to remove duplicates from an array?

Answer:

@array=(2,4,3,3,6,2);
my %seen=();
my @unique = grep { !seen{$_}++} @array;
print "@unique";

3) What is Perl on-liner?

Answer:
There are two ways a perl scripts can be run:
1. From a command line, called one-liner. It may contains many statements in one line.
e.g. perl -e "print \"Hello\";"

2. Script file, Perl program
e.g. perl program.pl

No comments:

Post a Comment

Search This Blog