My personal buzzphrase is to write your code in the domain of the solution rather than the problem. This is why duff's solution is better IMO than jZed's. It reads exactly the same way as what you think it should (which is a definite plus!), but it also reads the same as your question: "every element of the array being the [same type]". To get the type of the column with the column name, you just put "INT" or "INTEGER" in front. So you get "INT $column". You want to do that for each element in the array, so that's a map. You then want to join the columns, so that's a join.

Of course, if you later decide that there are some CHARs or something, and the default is INT, it's as easy as having a hash with the extra definitions, and modifying the map. jZed's solution, while working for your precise question, won't scale to new requirements (unless those new requirements are more INT columns).

my %defs = ( colC => { type => 'CHAR', len => '(90)' }, colH => { type => 'TIMESTAMP' } ); my $table_def = join ',', map { if ($defs{$_}) { "$defs{$_}{type} column $_" . ($defs{$_}{len} || '') } else { "INT column $_" } } @columns;
It's starting to get a bit long... but you could factor it out into its own function, and use map to call it for each function.

Strictly speaking, you don't seem to need this now. But I save a lot of time by writing code to prepare for the future. In my job, that can save a lot of time during the crunch at the end of a project. YMMV.


In reply to Re: A large table with DBI by Tanktalus
in thread A large table with DBI by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.