The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-03-2008
gctaylor gctaylor is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 5
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 (permalink)  
Old 09-04-2008
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149
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 (permalink)  
Old 09-04-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 715
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 (permalink)  
Old 09-04-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 09-05-2008
gctaylor gctaylor is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 5
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"

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0