CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Prase line with space and double quotes

 



ghat
New User

Apr 7, 2009, 7:17 AM

Post #1 of 4 (3206 views)
Prase line with space and double quotes Can't Post

Hi,

I have a config file lines with fields separated by space in the following format.

"NameA" "Values of A" "890"

"NameB" "Value of B" "450"


How do I get the fields in between the " "?. The result array should contain NameA,Value of A, 890 after parsing the first line.

thanks in advance.


PGScooter
stranger

Apr 7, 2009, 1:06 PM

Post #2 of 4 (3187 views)
Re: [ghat] Prase line with space and double quotes [In reply to] Can't Post

Hey ghat!

What have you tried so far? Can you pull out the first one at least (NameA)? Although there are a couple of ways to do it, I would suggest taking a look at lazy modifiers (or use a work around like [^"]). You'll also have to be careful not to match the space between the second and third quotes. Or, I guess you could match it and then get rid of it by checking which of your captured strings only have a space, but that seems less elegant :)

By the way, I'm a beginner so don't listen too much to what I say :)

good luck!
The more you teach me, the more I learn. The more I learn, the more I teach.


Shree
Novice


Apr 22, 2009, 2:08 AM

Post #3 of 4 (3083 views)
Re: [ghat] Prase line with space and double quotes [In reply to] Can't Post

Hi consider that I have a file called config.txt and the data lines in this file are as below:
-----------------------------------------------------------
"NameA" "Values of A" "890"
"NameB" "Value of B" "450"
-----------------------------------------------------------

As per your requirement the following code will work which stores the each values between double quotes as array elements

-----------------------------------------------------------
open(READFILE, "config.txt") || die "Cannot open the file $!\n";

while(<READFILE>) {

#$_ =~ s/\"//g;
#print $_;

my @values = split(/\" /,$_);

foreach my $index (@values) {

$index =~ s/\"//g;
print $index, "\n";
}

}
close(READFILE);
-----------------------------------------------------------
Thanks
-Shree



ichi
User

Apr 22, 2009, 5:00 AM

Post #4 of 4 (3079 views)
Re: [ghat] Prase line with space and double quotes [In reply to] Can't Post

better to use a ready made CSV parser for this. (i leave it to you to put in arrays)

Code
use Text::CSV; 
$csv = Text::CSV->new ( { sep_char => " " });
my $file = "file";
open (CSV, "<", $file) or die $!;
while (<CSV>) {
chomp;
if ($csv->parse($_)) {
foreach my $col ($csv->fields()){
print "col: " . $col. "\n";
}
} else {
my $err = $csv->error_input;
print "Failed to parse line: $err";
}
}
close(CSV);


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives