Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

tree-oriented query against table-oriented data

by dimar (Curate)
on Jul 01, 2006 at 17:27 UTC ( [id://558783]=perlquestion: print w/replies, xml ) Need Help??

dimar has asked for the wisdom of the Perl Monks concerning the following question:

Scenario:

Assume a table called 'tvshow' with the following data:
### begin_: tvshow table type ;; title ;; fname ;; lname sitcom ;; simpsons ;; homer ;; simpson sitcom ;; simpsons ;; ned ;; flanders sitcom ;; flintstones ;; fred ;; flintstone sitcom ;; flintstones ;; wilma ;; flintstone sitcom ;; flintstones ;; barney ;; rubble gameshow ;; price is right ;; bob ;; barker gameshow ;; jeopardy ;; alex ;; trebek

Question:

Does there exist anything that will allow that data to be queried in a manner such as (or substantially similar to):
### begin_: query pseudo-code TREE_SELECT type,title,fname,lname FROM tvshow WITH_LEVEL (1,'type' ) WITH_LEVEL (2,'title' ) WITH_LEVEL (3,'lname','fname' ) WHERE type = 'gameshow'
... producing the result ...
gameshow price is right barker,bob jeopardy trebek,alex

Caveats:

Assume the only important question is whether such a query syntax exists as either a published spec or (even better) runnable code somewhere.

It is nice to have a perl-based solution that returns the results in structured (e.g., XML, perl data structure, YAML) format, but for now, mere confirmation of whether this exists somewhere (and if so, how is it implemented and what is it called) would be enough. The purpose is due-dilligence before re-inventing this particular wheel, assuming it *is* a wheel.

=oQDlNWYsBHI5JXZ2VGIulGIlJXYgQkUPxEIlhGdgY2bgMXZ5VGIlhGV

Replies are listed 'Best First'.
Re: tree-oriented query against table-oriented data
by renodino (Curate) on Jul 01, 2006 at 19:45 UTC
    ...whether such a query syntax exists as either a published spec...

    (I'm assuming you're after SQL syntax here, tho its not explicitly stated)

    Tho not directly Perl related, there are implementations, and (conveniently) O'Reilly appears to have provided an answer as an online sample chapter for the SQL Pocket Guide.

    You may also find that careful use of GROUP BY will accomplish much of what you're trying to do, albeit with some duplicate info in the rows.

    A quick Google of "sql hierarchical queries" generates quite a few related hits, as does "sql tree queries", so you've got some websifting to do...

Re: tree-oriented query against table-oriented data
by esskar (Deacon) on Jul 01, 2006 at 19:05 UTC
    i guess, this will not exist, because it is actually a combination of querying data from a database and displaying the result. Looks like a problem for a MVC solution.
      i guess, this will not exist, because it is actually a combination of querying data from a database and displaying the result.

      That is one way of evaluating the problem, however it is possible to 'rethink' the situation if you strip away pre-existing paradigms, methodologies, and ask the following:

      Given this:

      sitcom/simpsons/homer/simpson sitcom/simpsons/ned/flanders sitcom/flintstones/fred/flintstone sitcom/flintstones/wilma/flintstone sitcom/flintstones/barney/rubble gameshow/price is right/bob/barker gameshow/jeopardy/alex/trebek

      What can I use to transform it into this?:

      $dataroot = { 'gameshow' => { 'price is right' => [ { 'lname' => 'barker', 'fname' => 'bob' } ], 'jeopardy' => [ { 'lname' => 'trebek', 'fname' => 'alex' } ] }, 'sitcom' => { 'simpsons' => [ { 'lname' => 'simpson', 'fname' => 'homer' }, { 'lname' => 'flanders', 'fname' => 'ned' } ], 'flintstones' => [ { 'lname' => 'flintstone', 'fname' => 'fred' }, { 'lname' => 'flintstone', 'fname' => 'wilma' }, { 'lname' => 'rubble', 'fname' => 'barney' } ] } };

      The key consideration here is a simple question of transforming the flat 'row-based' representation into a 'nested representation'. This does not necessarily involve a database at all. Nor does it necessarily involve 'displaying' anything (unless you count using Data::Dumper to show the output for illustrative purposes only).

      =oQDlNWYsBHI5JXZ2VGIulGIlJXYgQkUPxEIlhGdgY2bgMXZ5VGIlhGV

        Try this:

        #! perl -slw use strict; use List::Util qw[ reduce ]; use Data::Dumper; my %data; while( <DATA> ) { my( $genre, $title, $fname, $lname ) = split '/'; $data{ $genre } = {} unless exists $data{ $genre }; $data{ $genre }{ $title } = [] unless exists $data{ $genre }{ $tit +le }; push @{ $data{ $genre }{ $title } }, { fname => $fname, lname => $ +lname }; }; print Dumper \%data; __DATA__ sitcom/simpsons/homer/simpson sitcom/simpsons/ned/flanders sitcom/flintstones/fred/flintstone sitcom/flintstones/wilma/flintstone sitcom/flintstones/barney/rubble gameshow/price is right/bob/barker gameshow/jeopardy/alex/trebek

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        Just to refocus on the OP, the question was, (given the example 'one-off' solution that BrowserUk illustrated for this specific example, and considering the similarly-coded 'one-off' solutions by the OP in the past) where does there exist a "general purpose" mechanism to further abstract the process of doing this kind of transformation, such that there is no need to modify the 'one-off' code every time someone wants a different 'pivot' of the data structure.

        The question is now mostly academic, since the humble aspirant has gone on to code a solution himself, absent a preexisting and obvious choice out there.

        It is called 'vpath_pivot', named after the 'pivot table' feature found in spreadsheet software. It generates arbitrarily-nested tree structures using perl data variables, constructed from queries like:

        ### 1: provides same output as BrowserUk example TREE_SELECT genre,title,fname,lname FROM tvshow WITH_LEVELS ('genre','title' ) WITH_RECORD ( 'lname','fname' ) ### 2: a different 'pivot' of the same data TREE_SELECT title,genre,fname,lname FROM tvshow WITH_LEVELS ('title','genre','lname' ) WITH_RECORD ( 'fname' ) WHERE genre != 'gameshow' ### 3: another tree 'pivot' (more compact) TREE_SELECT title,fname,lname FROM tvshow WITH_LEVELS ('title' ) WITH_RECORD ( 'fname','lname' )

        NOTE: the above is not SQL, it just looks similar for readability.

        NOTE: the main approach here is to give the user the ability to 'slice and dice' the 'flat' data into different 'tree structures' all within a simple syntax (i.e., no need to re-edit the perl code).

        tyvm for the replies
        =oQDlNWYsBHI5JXZ2VGIulGIlJXYgQkUPxEIlhGdgY2bgMXZ5VGIlhGV

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://558783]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-19 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found