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).
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.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;
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |