From: Thomas Andersson on
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

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