Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 14th, 2008, 05:17 AM
Newbie
 
Join Date: Jul 2008
Posts: 13
Default How to substitute the space for field of csv format?

Currently my data is this in excel but as a csv format:

"a", "as " sd", "123"
"b","asd","234"
"c","as d","345"

I wanted to make the output this:
a,as " sd,123
b,asd,234
c,as d,345

the code i tried is this

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use Text::CSV_XS;
  3.  
  4. my $csv = Text::CSV_XS->new({'allow_whitespace'=>1,'allow_loose_quotes'=>1});
  5. my $file = "test.csv";
  6.  
  7. open(DAT, $file) || die("Cannot Open File");
  8.  
  9. while (<DAT>) {
  10.     $csv->parse($_);
  11.     my @fields = $csv->fields();
  12.     foreach ("@fields")
  13.     {
  14.         $_ =~ s/ +/,/;
  15.         print "$_\n";
  16.     }
  17. }
  18.  
How can i modify to get what I desired?
Reply
  #2  
Old August 15th, 2008, 04:39 PM
Newbie
 
Join Date: Jul 2008
Posts: 14
Default

In your code at line number 14. You need to use \s+ for one or more space character.
so it will be
Expand|Select|Wrap|Line Numbers
  1. $_ =~ s/\s+/,/;
Hope this works
Reply
Reply

Bookmarks

Thread Tools

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 Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles