Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How to represent complex data structure and manipulate it easily?

by Hanken (Acolyte)
on Jun 19, 2012 at 01:35 UTC ( #976939=perlquestion: print w/ replies, xml ) Need Help??
Hanken has asked for the wisdom of the Perl Monks concerning the following question:

Hi Dear Perl Experts,

I got a problem about how to represent a complex data structure in perl. Here is a rough introduction of my data: (1) the data consist of several commands: cmd00, cmd01, cmd02... And each command has a respective table; (2) the table has 3 elements: initial state, feedback and end state; (3) initial state could be any subset of {idle, start, jump, halt}; for each initial state, feedback has to pick up one from number (0 .. 3); (4) each initial state and feedback have a certain relative end state which is also within {idle, start, jump, halt}; end state could be more than one elements;

Graphically and strictly, it is showed as below:
Command              Initial state.           Feedback.              E +nd state cmd00.                    Idle                      0.                 +            Idle or start                                                             1.         +                    Start                                 Start.                   0.           +                  Jump                                                             3.         +                    Halt or start                                                             2.         +                     Idle cmd01                     Idle                      0.                 +            Idle or start                                                             1.         +                    Jump                                 Jump                   0.             +                Halt or idle                                                             1.         +                    start                                                             2.         +                     Idle cmd02.  ....... ......

My target: I need a function like sub get_endstate_by_cmd($cmd, $default_state); I will send a random sequence of commands to the sub routine in which initial state and feedback will be randomized in their legal range, then it returns the end state for next iteration. The difficulties are: 1. To represent the data; 2. To randomize in a legal range; Could anyone give me some hints?

Comment on How to represent complex data structure and manipulate it easily?
Download Code
Re: How to represent complex data structure and manipulate it easily?
by roboticus (Canon) on Jun 19, 2012 at 02:51 UTC

    Hanken:

    Representing the data can be a bit tricky. But choosing the best "shape" for the data really depends on how you're using it. I'd suggest sketching out the code for the problem you're solving, and see what sorts of access methods you need. That will help you select a good representation of the data. You might want to review the perl data structures cookbook (perldoc perldsc) before and after sketching out your ideas, just to help you make associations.

    As far as choosing inside a legal range, the rand function (perldoc -f rand) should get you going. You might want to print out the first page of perlfunc as a handy list of the various types of functions built into perl.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: How to represent complex data structure and manipulate it easily?
by Anonymous Monk on Jun 19, 2012 at 03:04 UTC

    Well, lines means array, and rows/columns means array-of-arrays

    my @Commands = my @lines = [ qw/ Command Initial Feedback End / ];

    or if you need names, array-of-hashes

    = { map {; $_ => $_ } qw/ Command Initial Feedback End / };

    or if you need names and space savings of array

    use constant do { my $ix = 0; +{ map { $_ => $ix++ } qw[ Q_COMMAND Q_INITIAL Q_FEEDBACK Q_END ] } }; ... die $command->[ Q_FEEDBACK() ];
Re: How to represent complex data structure and manipulate it easily?
by NetWallah (Abbot) on Jun 19, 2012 at 05:21 UTC
    This sounds like a job for a Finite state machine like DMA::FSM.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

Re: How to represent complex data structure and manipulate it easily?
by linuxkid (Sexton) on Jun 19, 2012 at 20:26 UTC

    I suggest something like this:$structure->[$number]->{$name}

    --linuxkid


    imrunningoutofideas.co.cc
      Did you know you can omit the second (and subsequent) arrows?
      $structure->[$number]->{$name} can be written $structure->[$number]{$name}.


      - Boldra

        never did realize that, but i dok find it improves readability, especcially when helping grren perl converts...

        --linuxkid


        imrunningoutofideas.co.cc

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2014-12-27 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls