Perl: array, assigning multi-word sentences with quotes

Just wondering if there's a better way to get these complete sentences into an array and keep the quotes intact? All the quotes make it look ugly to me but it works. I want to be able to refer to the full sentences by index. I've tried a few qw and qq/ aproaches but what I have below seems about the only way I can get it to work. Most examples I've seen with assigning in to arrays only show single words. I was thinking about using something with split but seems more trouble than it's worth. This isn't critical just trying to learn.

my @arrayquotes = ("\"An Apple a day keeps the doctor away\"", "\"Procastination is the thief of time\"" );

print "@arrayquotes\n";
print "$arrayquotes[0]\n";

Thanks.

I'm not sure this is better, but it at least gets rid of the backslashes.

my @arrayquotes = ('"An Apple a day keeps the doctor away"', '"Procastination is the thief of time"');

Hi.

This is another technique:

#!/usr/bin/perl

# @(#) p2       Demonstrate convenient preservation of quotes on strings.

use warnings;
use strict;

my($debug);
$debug = 0;
$debug = 1;

# Put all strings here on separate lines.
# Note that later the newline will be used as a separator

my($a) = qq(
"A or b"
"C and d"
'E and f'
);

print " scalar a is $a";

my(@a) = split("\n",$a);

print "\n";
for my $sentence ( @a ) {
  next if $sentence eq "";
  print " sentence is $sentence\n";
}

exit(0);

Producing:

% ./p2
 scalar a is
"A or b"
"C and d"
'E and f'

 sentence is "A or b"
 sentence is "C and d"
 sentence is 'E and f'

All the strings are placed into a scalar, with the separator being a newline, then the scalar is split into the array.

Because there is some processing, you could also place quotes around the strings later, saving some keystrokes on entry. However, then you would lose the flexibility of placing any characters of your choice around the strings -- I used both single and double quotes above, for example.

It is convenient if your sentences are short. If you had long sequences, you'd need to invent a continuation character to allow lines to be joined ... cheers, drl

How about this?

@arrayquotes = split ("\n", <<HERE);
"An array a day keeps the doctor away"
"Foo is the new bar."
"I have seen the fnords."
HERE

It's the same idea as drl's but somewhat more compact IMHO, and avoids the multi-line qq() which I believe is considered somewhat obsolescent.

Thanks for the replies. It's educational to see the different approaches used. What's interesting to me is that when I use this approach I get the newline as index 0. How would I get rid of that? I tried delete $a[0], chomp, and finally chop.

#!/usr/bin/perl 

my $a = qq(
"A or b"
"C and d"
'E and f'
);

print " scalar a is $a";

my(@a) = split("\n",$a);

print " array a is @a\n";
print " index 0 is $a[0]\n";
print " index 1 is $a[1]\n";

Here's my output:

 scalar a is 
"A or b"
"C and d"
'E and f'
 array a is  "A or b" "C and d" 'E and f'
 index 0 is 
 index 1 is "A or b"