![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Assigning the values to an Array | kkraja | Shell Programming and Scripting | 1 | 08-11-2008 07:28 AM |
| perl: Assigning array values.. | looza | Shell Programming and Scripting | 4 | 07-15-2008 09:08 AM |
| Displaying double quotes using Perl | kbdesouza | Shell Programming and Scripting | 2 | 12-10-2007 11:18 AM |
| Assigning values to an array | yongho | UNIX for Dummies Questions & Answers | 4 | 07-13-2005 09:49 PM |
| PERL, extract value between double quotes | methos | Shell Programming and Scripting | 6 | 06-06-2003 01:24 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. Code:
my @arrayquotes = ("\"An Apple a day keeps the doctor away\"", "\"Procastination is the thief of time\"" );
print "@arrayquotes\n";
print "$arrayquotes[0]\n";
Thanks. |
|
||||
|
How about this? Code:
@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. Code:
#!/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: Code:
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" |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|