# Defining a data structure - is there a standard/best practice way?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

I am writing a module which interacts with a database. It is only
able to perform certain types of query, and return data in certain
formats (I'm not opening up the whole of DBIx for users of the
module - if they want to do more then my module isn't for them).

So the user creates a new query using my module:
my $q = Module::DB::Query->new({ type => 'select', table => 'products', cols_wanted => [qw/foo bar baz/], criteria => [ { column => 'id', value => 7, operator => '>' }, { column => 'description', value => 'lorem', operator => '=' }, ], return => 'aoa' # array of arrays }); I'm looking for a way to describe all the permutations for the different queries my module can handle, in a way that can be used to test that I have all the data I need to run the query. I started with this: my %supported = { select => { table => 'SCALAR', cols_wanted => 'ARRAY', criteria => [{ column => 'SCALAR', value => 'SCALAR', operator => 'SCALAR' }], } update => { } insert => { } } But as you can see it very soon breaks down. There's no way in my scheme for 'criteria' to be specified as an array of hashes. I'm sure this problem must have come up before and a solution exist. Maybe I'm just thinking about this the wrong way, and someone could steer my thinking in the right way? Thank you for your suggestions. Justin. -- Justin C, by the sea. ## Re: Defining a data structure - is there a standard/best practice way? On Monday, November 11, 2013 5:22:44 AM UTC-5, Justin C wrote: I frequently have great big globs of data that I need to print/export in pa rticular ways. Aside from the fact that the requirements of your printing/e xporting activity is EXTREMELY important, I customarily start with stuffing all the data in a single hash \ref. I then print the hashref in a simple A SCII file, which allows me to look at the data and determine how I want to print it out. For example, a very simple example, suppose my data file looks like this: ID,NAME,COLLEGE,PROGRAM,LEVEL,TERM,COURSE,GPA,CREDITS,RESTRICTIONS use Text::ParseWords; my %bighash; #first, grab all the data and stuff it into memory while (<IN>) { my($ID,$NAM,$COL,$PRO,$LEV,$TER,$COU,$GPA,$CRE,$RES)$bighash = {
ID  => $ID, NAM =>$NAM,
COL => $COL, PRO =>$PRO,
LEV => $LEV, TER =>$TER,
COU => $COU, GPA =>$GPA,
CRE => $CRE, RES =>$RES, };
}
# now, print all the data out
while (<OUT>)
{
foreach my $COL (sort keys %bighash) { foreach my$LEV (sort keys %}) {
foreach my $PRO (sort keys %}) { foreach my$TER (sort keys %) {
foreach my $key (sort keys %) { print OUT "$key =>$bighash$COL}\n";
}}}}}}

Yeah, I know this is mindless, but that's the point. Once you do the mindle
ss part, you can study your output requirements and construct one or more h
ashes to conveniently output what you need.

Anyway, this technique works for me, I use it almost every day.

CC.