Greetings, I'm reading in a file containing a single column (representing column names in a table) I.e, FIELD1 FIELD2 FIELDN (each on a separate line) I would like to pass the file into an array and apply work to the array values such that the end result would be a single line 'FIELD1','FIELD2','FIELDN' to be passed in a data base query. I've tried working outside of perl via my $table_fieldname_array=`cat $column_list|xargs|sed -e 's/ /'"'"','"'"'/g'`; which writes the contents of the file as FIELD1','FIELD2','FIELDN which I then attempt to concatenate in perl with (' and ') However the trailing ') ends up on a separate line. My question is two fold; 1 how can i concatenate my string with(' and ') without the ') appearing on a separate line?

# work around my $column_list=foofile.txt my $table_fieldname_list=`cat $column_list|xargs|sed -e 's/ /'"'"','"' +"'/g'`; print "see what it looks like: $table_fieldname_list\n"; $table_fieldname_list= "('" . $table_fieldname_list; $table_fieldname_list = $table_fieldname_list . "')"; print "concatenatd table list $table_fieldname_list\n"; # ends up #('FIELD1','FIELD2','FIELDN # ')

2, what might be the perl only solution to read in a file to an array and convert contents of the array to a single line in quotes and separated by ,?

# Perl code that can be enhanced? my $column_list=foofile.txt open (my $fh,"<","$column_list") or die "cannot open < $column_list: $!"; my @table_fieldname_array = <$fh>; # see what it looks like print "field array @table_fieldname_array\n"; # do something to the array for example foreach my $attr_name (@table_fieldname_array ) { $field_list .= $attr_name . ","; } print "the result is the array on separate lines with a trailing , ... +not useful yet @table_fieldname_array\n";

In reply to wrap contents of an array in single quotes separated by comma by perl197

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.