Perl Programming Question:

Download Job Interview Questions and Answers PDF

How do I print the entire contents of an array with Perl?

Perl Programming Interview Question
Perl Programming Interview Question

Answer:

To answer this question, we first need a sample array. Let's assume that you have an array that contains the name of baseball teams, like this:
@teams = ('cubs', 'reds', 'yankees', 'dodgers');
If you just want to print the array with the array members separated by blank spaces, you can just print the array like this:
@teams = ('cubs', 'reds', 'yankees', 'dodgers');
print "@teamsn";
But that's not usually the case. More often, you want each element printed on a separate line. To achieve this, you can use this code:
@teams = ('cubs', 'reds', 'yankees', 'dodgers');
foreach (@teams) {
print "$_n";
}

Download Perl Programming Interview Questions And Answers PDF

Previous QuestionNext Question
How do you match one letter in the current locale?How many ways can we express string in Perl?