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 | |
by perl197 (Novice) on Feb 21, 2015 at 17:57 UTC | |
|
Re: wrap contents of an array in single quotes separated by comma
by Discipulus (Canon) on Feb 21, 2015 at 22:13 UTC |