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

Discussion in 'Perl Misc' started by Justin C, Nov 11, 2013.

  1. Justin C

    Justin C Guest

    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.
    Justin C, Nov 11, 2013
    #1
    1. Advertising

  2. Justin C

    ccc31807 Guest

    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,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{$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.
    ccc31807, Nov 11, 2013
    #2
    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. Patrick.O.Ige
    Replies:
    0
    Views:
    396
    Patrick.O.Ige
    Sep 30, 2005
  2. ׿ǿ Zhuo, Qiang

    What's The Best Practice Defining Error Codes in C

    ׿ǿ Zhuo, Qiang, Nov 16, 2008, in forum: C Programming
    Replies:
    2
    Views:
    1,755
    Zhuo, Qiang ׿ǿ
    Nov 17, 2008
  3. oldyork90
    Replies:
    1
    Views:
    154
    Jeremy J Starcher
    Sep 10, 2008
  4. rammy
    Replies:
    16
    Views:
    1,680
    Tim Rentsch
    Jul 2, 2012
  5. Sergi Pasoev
    Replies:
    0
    Views:
    372
    Sergi Pasoev
    Jun 29, 2012
Loading...

Share This Page