You probably don't want the trailing comma but it is easy to add back in with the solution below if required. The easiest way to print an array with a specific separator is to change the default list separator variable ($") from it's normal value of a space to the required character or string. This demonstrates the behaviour.

$ perl -e' -> @l = (1, 2, 3); -> print @l, qq{\n}; -> print qq{@l\n}; -> $" = q{,}; -> print qq{@l\n};' 123 1 2 3 1,2,3 $

Note that printing the array outside quotes just concatenates the elements together whereas inside quotes separates them with the default space and once we change the default to a comma, voila.

In your particular case you also want to quote each field, which you can do in a map. We can create a new list to print, like this

use strict; use warnings; my @flds = qw{Leesville TX 23432 67867 88 Bloggs F}; my @quotedFlds = map { qq{"$_"} } @flds; { local $" = q{,}; print qq{@quotedFlds\n}; }

Note that I localise the change to $" in a small code block to avoid unexpected side-effects later in the script; localising changes like this is always good policy. Here's the output

"Leesville","TX","23432","67867","88","Bloggs","F"

I hope this is of use.

Cheers,

JohnGG


In reply to Re^4: loop through values in a string and print " " around each value. by johngg
in thread loop through values in a string and print " " around each value. by kevyt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.