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: Intermediate:
string processing question

 



cmm123
New User

Apr 5, 2013, 6:31 AM

Post #1 of 3 (663 views)
string processing question Can't Post

Hello
I have a programming question.. looking to do this in an elegant way using reg-ex if possible...

Here is the scenario.. I have lines of data I need to process - which ultimately I want to use the 'split' cmd on (space delimited). However, some fields have spaces in the field - these fields will be encapsulated with double quotes. Any field may be a " ... " field - and the number of fields on a line can be variable.

I know how to do this if there is only one " " field on the line... when there is 1 or more of those fields allowed, that's where I get stuck.

2 Example data lines:

AA_1 "field with spaces" BB_3 CC_5 AB_2 DD_4
"this has spaces" ZZ_3 "more spaces here" CC_1

I'd like to transform the lines above to:
AA_1 field##with##spaces BB_3 CC_5 AB_2 DD_4
this##has##spaces ZZ_3 more##spaces##here CC_1

So, I just want to replace any / all " .." encapsulated field(s) with the field - each space replaced by ##, and remove the leading and trailing double quotes.

Once I have this, I can easily do a 'split' on the lines - w/ space as the delimiter. And once I have the entries in the array, I'll simply translate the ## back to a space character ( I know there cannot be ## in any incoming field in the string).

Thanks...


FishMonger
Veteran / Moderator

Apr 5, 2013, 7:21 AM

Post #2 of 3 (659 views)
Re: [cmm123] string processing question [In reply to] Can't Post

That would be a very messy approach. It's better to use the Text::CSV module.
http://search.cpan.org/~makamaka/Text-CSV-1.21/lib/Text/CSV.pm

example

Code
#!/usr/bin/perl 

use strict;
use warnings;
use Text::CSV;
use Data::Dumper;

my $csv = Text::CSV->new({sep_char => ' '});

while ( my $row = $csv->getline(*DATA) ) {
print Dumper $row;
}

__DATA__
AA_1 "field with spaces" BB_3 CC_5 AB_2 DD_4
"this has spaces" ZZ_3 "more spaces here" CC_1


output

Code
$VAR1 = [ 
'AA_1',
'field with spaces',
'BB_3',
'CC_5',
'AB_2',
'DD_4'
];
$VAR1 = [
'this has spaces',
'ZZ_3',
'more spaces here',
'CC_1'
];



cmm123
New User

Apr 5, 2013, 8:47 AM

Post #3 of 3 (653 views)
Re: [FishMonger] string processing question [In reply to] Can't Post

Thanks!! I will give that a try... a lot cleaner approach for sure.

 
 


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

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