Perl: array, assigning multi-word sentences with quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: array, assigning multi-word sentences with quotes
# 1  
Old 09-03-2008
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.
# 2  
Old 09-04-2008
I'm not sure this is better, but it at least gets rid of the backslashes.
Code:
my @arrayquotes = ('"An Apple a day keeps the doctor away"', '"Procastination is the thief of time"');

# 3  
Old 09-04-2008
Hi.

This is another technique:
Code:
#!/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:
Code:
% ./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
# 4  
Old 09-04-2008
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.
# 5  
Old 09-05-2008
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"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : Assigning multile hash values to a single array

I know that @food = %fruit; Works. But how do I assign %fruit and %veggies to @food ? (2 Replies)
Discussion started by: popeye
2 Replies

2. Shell Programming and Scripting

Reading from a file and assigning to an array in perl

I wrote a simply perl that searched a file for a particualr value and if it found it, rite it and the next three lines to a file. Now I have been asked to check those next three lines for a different value and only write those lines if it finds the second value. I was thinking the best way to... (1 Reply)
Discussion started by: billprice13
1 Replies

3. Shell Programming and Scripting

extracting sentences that only contain a word

Hi guys Need your help how do I extract sentences with only a word i.e. today is hot hot very humid humid2 Sample output hot (6 Replies)
Discussion started by: jamestan
6 Replies

4. UNIX for Dummies Questions & Answers

extracting sentences that only contain a word

Hi guys Need your help how do I extract sentences with only a word i.e. today is hot hot very humid humid2 Sample output hot very (0 Replies)
Discussion started by: jamestan
0 Replies

5. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

6. Shell Programming and Scripting

Perl : Search for next occurence of a word in an array

I have an array as follows: Space: ABC Name: def Age: 22 Type: new Name: fgh Age: 34 Type: old Space: XYZ Name: pqr Age: 44 Type: new : : How can I separate the array with elements starting from Space:ABC until Space: XYZ & put them in a different array & so on... (4 Replies)
Discussion started by: deo_kaustubh
4 Replies

7. Shell Programming and Scripting

How to match all array contents and display all highest matched sentences in perl?

Hi, I have an array with 3 words in it and i have to match all the array contents and display the exact matched sentence i.e all 3 words should match with the sentence. Here are sentences. $arr1="Our data suggests that epithelial shape and growth control are unequally affected depending... (5 Replies)
Discussion started by: vanitham
5 Replies

8. Programming

How to extract a sentences of word from a text file.

Hi , i have a text file that contain a story How do i extract the out all the sentences that contain the word Mon. in C++ I only want to show those sentences that contain the word mon eg. Monkey on a tree. Rabbit jumping around the tree. I am very rich, I have lots of money. Today... (1 Reply)
Discussion started by: xiaojesus
1 Replies

9. Shell Programming and Scripting

Assigning the values to an Array

hi every body, i donot know how to assign a array varible with a file see i having file more file property1 Name property2 Address the above two line are tab Space seperated between the property and its value i want to seperate it and assign to... (1 Reply)
Discussion started by: kkraja
1 Replies

10. Shell Programming and Scripting

perl: Assigning array values..

I have to add a variable value to an array, something like this: ...... @my_array_name = $value_of_this_variable; This doesnt seem to work, any ideas why? Thanks! (4 Replies)
Discussion started by: looza
4 Replies
Login or Register to Ask a Question