Prev: Spreadsheet shows scientific notation occasionally with some cells, but it is wrong
Next: FAQ 8.23 How can I open a pipe both to and from a command?
From: Thomas Andersson on 3 Aug 2010 22:53 I there an easy way to stora all elements of an array tab delimitered in a string (need to prepare collected data for import to a database).?
From: Ben Morrow on 3 Aug 2010 23:10 Quoth "Thomas Andersson" <thomas(a)tifozi.net>: > I there an easy way to stora all elements of an array tab delimitered in a > string (need to prepare collected data for import to a database).? perldoc -f join If your elements might contain tabs, you are working with a variant of CSV. Use Text::CSV_XS. Ben
From: Sherm Pendley on 3 Aug 2010 23:18 "Thomas Andersson" <thomas(a)tifozi.net> writes: > I there an easy way to stora all elements of an array tab delimitered in a > string (need to prepare collected data for import to a database).? join() is the opposite of split(). my $record = join("\t", @fields); However! Note that this simple approach is fragile - if any of your fields contain tab characters, it will break. You might want to use map() to quote each field before joining them together: my $record = join("\t", map("\"$_\"", @fields)) That can break too, if your fields can contain quotes *and* tabs. There comes a point where it's easier to use DBI to connect to your database and insert your data directly. :-) sherm-- -- Sherm Pendley <www.shermpendley.com> <www.camelbones.org> Cocoa Developer
From: RedGrittyBrick on 4 Aug 2010 05:41 On 04/08/2010 04:18, Sherm Pendley wrote: > "Thomas Andersson"<thomas(a)tifozi.net> writes: > >> I there an easy way to stora all elements of an array tab delimitered in a >> string (need to prepare collected data for import to a database).? > > my $record = join("\t", @fields); > > However! Note that this simple approach is fragile - if any of your > fields contain tab characters, it will break. You might want to use > map() to quote each field before joining them together: > > my $record = join("\t", map("\"$_\"", @fields)) > > That can break too, if your fields can contain quotes *and* tabs. There > comes a point where it's easier to use DBI to connect to your database > and insert your data directly. :-) Since database tools for loading data often allow you to specify a separator character, I sometimes find it easier to find a character that isn't present in the data. I haven't been so adventurous as to use the ASCII field-separator character (FS) but often use a vertical bar (|). Just my ยค0.02 worth -- RGB
From: Thomas Andersson on 4 Aug 2010 09:33
Sherm Pendley wrote: >> I there an easy way to stora all elements of an array tab >> delimitered in a string (need to prepare collected data for import >> to a database).? > > join() is the opposite of split(). > > my $record = join("\t", @fields); > > However! Note that this simple approach is fragile - if any of your > fields contain tab characters, it will break. You might want to use > map() to quote each field before joining them together: > > my $record = join("\t", map("\"$_\"", @fields)) > > That can break too, if your fields can contain quotes *and* tabs. > There comes a point where it's easier to use DBI to connect to your > database and insert your data directly. :-) Thank you, my array will contain single words or numbers only so shouldn't be a problem. Might colons be a problem? (I know my script fails at collecting the fields containing a time ref and I assume it's due to the colon). Best Wishes Thomas |