1. Home
  2. Computing & Technology
  3. Perl

Beginning Perl Tutorial - Data Types: Lists
More on Quoted Strings

by Kirk Brown
for About.com

Next: Playing With Lists

We started working with strings in the scalar tutorial. Since a list is just an ordered group of scalars, you can also create lists of strings. In fact, we've already seen some examples of this at the start of the tutorial. Just like a string going into a scalar, you use the single or double quotes to contain it.

@myNames = ('Larry', 'Curly', 'Moe');
The same rules for single and double quote apply to strings in lists.
#!/usr/local/bin/perl
@myStrings = (
'It\'s necessary to escape the single quote.',
"It's fine to leave a single quote free.\n A \" and a \\ must be escaped.\n"
);
print @myStrings;
Try running the program, and you'll see that it prints out exactly as the individual strings in the other tutorial.

There's another handy shorthand you can use for lists of words that can save some typing. Rather than assigning the array like this:

@myNames = ('Larry', 'Curly', 'Moe');
Use the quote word shorthand that splits the data in the parentheses by spaces:
@myNames = qw(Larry Curly Moe);
Next: Playing With Lists
Explore Perl
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Perl
  4. Perl Tutorials
  5. Beginning Perl Tutorial - Data Types: Lists>

©2009 About.com, a part of The New York Times Company.

All rights reserved.