Storing array elements tab delimitered in a string

Discussion in 'Perl Misc' started by Thomas Andersson, Aug 4, 2010.

  1. 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).?
     
    Thomas Andersson, Aug 4, 2010
    #1
    1. Advertising

  2. On 04/08/2010 04:18, Sherm Pendley wrote:
    > "Thomas Andersson"<> 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
     
    RedGrittyBrick, Aug 4, 2010
    #2
    1. Advertising

  3. 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
     
    Thomas Andersson, Aug 4, 2010
    #3
  4. bugbear 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).?

    >
    > 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.
     
    Thomas Andersson, Aug 4, 2010
    #4
  5. Sherm Pendley wrote:

    >> Thank you, my array will contain single words or numbers only so
    >> shouldn't be a problem. Might colons be a problem?

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

    >> (I know my script fails at
    >> collecting the fields containing a time ref and I assume it's due to
    >> the colon).

    >
    > 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.
     
    Thomas Andersson, Aug 4, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hermit Dave
    Replies:
    0
    Views:
    460
    Hermit Dave
    Jan 22, 2004
  2. News Sympatico
    Replies:
    0
    Views:
    319
    News Sympatico
    May 21, 2005
  3. toton
    Replies:
    11
    Views:
    728
    toton
    Oct 13, 2006
  4. Jonathan Wood
    Replies:
    1
    Views:
    524
    Jonathan Wood
    Jun 2, 2008
  5. Thomas Andersson
    Replies:
    20
    Views:
    311
Loading...

Share This Page