Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Defining a data structure - is there a standard/best practice way?

Reply
Thread Tools

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

 
 
Justin C
Guest
Posts: n/a
 
      11-11-2013

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:B::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.
 
Reply With Quote
 
 
 
 
ccc31807
Guest
Posts: n/a
 
      11-11-2013
On Monday, November 11, 2013 5:22:44 AM UTC-5, Justin C wrote:
> 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).


I frequently have great big globs of data that I need to print/export in particular ways. Aside from the fact that the requirements of your printing/exporting activity is EXTREMELY important, I customarily start with stuffingall the data in a single hash \ref. I then print the hashref in a simple ASCII 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,CRED ITS,RESTRICTIONS

use Text:arseWords;
my %bighash;
#first, grab all the data and stuff it into memory
while (<IN>)
{
my($ID,$NAM,$COL,$PRO,$LEV,$TER,$COU,$GPA,$CRE,$RE S)
$bighash{$COL}{$LEV}{$PRO}{$TER} = {
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 %{$bighash{$COL}}) {
foreach my $PRO (sort keys %{$bighash{$COL}{$LEV}}) {
foreach my $TER (sort keys %{$bighash{$COL}{$LEV}{$PRO}) {
foreach my $key (sort keys %{$bighash{$COL}{$LEV}{$PRO}{$TER}) {
print OUT " $key =>$bighash$COL}{$LEV}{$PRO}{$TER}{$KEY}\n";
}}}}}}

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

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

CC.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What's The Best Practice Defining Error Codes in C rammy C Programming 16 07-02-2012 02:38 AM
Re What's The Best Practice Defining Error Codes in C Sergi Pasoev C Programming 0 06-29-2012 09:15 PM
What's The Best Practice Defining Error Codes in C ׿ǿ Zhuo, Qiang C Programming 2 11-17-2008 04:16 PM
hidden data - best practice, best way, suggestions oldyork90 Javascript 1 09-10-2008 07:46 PM
Remember when your piano teacher taught you, "Practice, practice,practice ...?" Wayne Wastier Windows 64bit 3 06-10-2005 08:29 PM



Advertisments