perl197 has asked for the wisdom of the Perl Monks concerning the following question:

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";

Replies are listed 'Best First'.
Re: wrap contents of an array in single quotes separated by comma
by AppleFritter (Vicar) on Feb 21, 2015 at 17:35 UTC

    The key is to chomp your data to eliminate trailing newlines. You can also use join to concatenate the array elements with a specified separator. Here's how I might do it:

    #!/usr/bin/perl use strict; use warnings; use feature qw/say/; chomp(my @fields = <DATA>); my $table_fieldname_array = "('" . join("','", @fields) . "')"; say $table_fieldname_array; __DATA__ FIELD1 FIELD2 FIELDN

    This outputs:

    $ perl 1117442.pl ('FIELD1','FIELD2','FIELDN') $

    Which I gather is what you want.

      You gathered correctly indeed that's what i'm looking to accomplish. I was employing join in other attempts but still fell short. Thank-you.

Re: wrap contents of an array in single quotes separated by comma
by Discipulus (Canon) on Feb 21, 2015 at 22:13 UTC
    This is almost a oneliner task, and note that mine is not an elegant solution
    win>perl -lne "push @arr, $_; END{print join ',', map{'\''.$_.'\'' } +@arr }" trash.txt #untested, only different quotation. linux>perl -lne 'push @arr, $_; END{print join ",", map{"'".$_."'" } +@arr }' trash.txt

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.