how to retain double quotes in perl

hello experts...
I have a scalar string with certain string  as value.But if my string is double quoted i want that double quote to be retained when i write that string into a  csv file.My prog is as follows...

 #!/usr/bin/perl

  open("fh",">>i.csv");

  my $data ='dave:"david,de":chris';

  my @values = split(':', $data);

  foreach my $val (@values) {
    print fh "\"$val\"\n";

}
  close(fh);
  exit 0;

Here i want "david,de" to be printed within one cell of the excel sheet with the double quotes.
Please help...
gaugetaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wilcoxonCommented:
To print quotes, Excel wants triple sets so, this should work:
#!/usr/bin/perl

  open("fh",">>i.csv");

  my $data ='dave:"david,de":chris';

  my @values = split(':', $data);

  foreach my $val (@values) {
    $val =~ s{"}{"""}g;
    print fh "$val\n";

}
  close(fh);
  exit 0;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gaugetaAuthor Commented:
@wilcoxon::Thank you very much.Can you describe what's happening in the code.It worked perfectly.And i am a java programmer and a newbie in perl.Can you suggest some study material like perl for java programmers.Thanks for helping...
wilcoxonCommented:
Here's the code commented with what's going on and a few minor modifications...
#!/usr/bin/perl

# you should always use strict and warnings
# it will greatly reduce errors in code.
use strict;
use warnings;

# quotes around fh is unnecessary
# convention is to upper-case handle globs
# (use warnings will complain about lower-case)
open(FH,">>i.csv");

# put the data into the var
my $data ='dave:"david,de":chris';

# split the variable into its parts on :
# convention is to use regex (but it works either way)
my @values = split(/:/, $data);

# loop over the values
foreach my $val (@values) {
    # replace any occurrence of " in the string with """
    $val =~ s{"}{"""}g;
    # print the (possibly modified) value out
    print FH "$val\n";
}

close(FH);
# 0 is unnecessary on exit
# exit at the end of a program is unnecessary
exit 0;

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.