Storing array elements tab delimitered in a string

  • Thread starter Thomas Andersson
  • Start date
T

Thomas Andersson

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).?
 
R

RedGrittyBrick

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
 
T

Thomas Andersson

Sherm said:
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
 
T

Thomas Andersson

bugbear said:
What import formats does your database support?

MS Access 2007, the import itself should be a problem. I also realized that
the array was not neccessary anyway, now I just concatenate the collected
data with tabs inserted to a string that I later write out with linefeed to
a txt file.
 
T

Thomas Andersson

Sherm said:
Not if you're using tabs as field separators. The general pattern is
that whatever characters you're assigning special meaning to - i.e.
tabs as field separators, quote marks as string delimiters, etc. -
need to be handled differently whenever they are used as "normal"
characters instead.

Turns out the colon wasn't the problem, the cell contained more data than I
knew so now I have to figure out a new solution.
That's why there comes a point where it's easier to just use DBI and
insert the data directly - exporting to a text file can be easier in
the simplest case, but grows in complexity if you start having to
handle a bunch of special cases and escape sequences.

That will be added as I learn, trying to start simple at first, remember a
week ago I had never seen a line of perl code in my life.
More likely, it's due to the database expecting a different format;
for example, if it's a datetime field in MySQL, you can't supply just
the time by itself. Or, if it's expecting YY/MM/DD, and you give it a
date that's formatted as 12/20/67, it will see that date as being
invalid.

I'd need to see the field definition, and an example of a failed input
to tell for sure though - without that, all I can offer is vague gen-
eralizations.

I'm using HTML::TreeBuilder to create a html tree to navigate through by
feeding my data collector sub the coordinates of my data. It works fine for
all cells containing strings of text or numbers, what caused it to fail was
that the meanies had inserted the date in a link within the cell. Now I need
figure out how to only collect the interesting data and bypass the link.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top