Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Help with ADT

by dimmesdale (Friar)
on Jul 24, 2002 at 19:36 UTC ( [id://184993]=perlquestion: print w/replies, xml ) Need Help??

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

Hmm... perl's ease has led me down a horrible path yet again. Well not so horrible, but you'll see.

I come from a C/C++ background, so when I want to create an ADT (advanced data type), I use struct or class. It's so easy.

I want to create a heterogenous data structure in perl, and I would appreciate some input -- currently I'm getting an error about coercing a hash into an array (or was it an array into a hash ... :)

Here's a quick summary of what I need:

Example Keep track of Varies by
0 Index Subdirectory ... File ... Measurment
ParserObject1 Parser Subdirectory ... File
HRT Name Measurement
Heart Rate Label Measurement
PRFN1 Conditions constant
Low-Hour VFR Groups constant
Heart rate < 50 Invalid data constant
0404 -> 1 Substitutions constant

For the invalid data, I thought that the value could be something like this --

[ ['measurement1', [ ['test subject number', 21, 27], ['value', 50], ... ], ... ], ... ]

Where 21 was the min, 27 the max, etc.

I'm not sure that any of that is needed by you, so I'll stop there.

However, my current method of referring to these data is as such:

$info->{subdirectory}->{file}->[measurement] = INDEX; $info->{subdirectory}->{file} = PARSER; $info->[measurement]->{name} = NAME; ...etc., according to what it varies by...

My question to you: Is this, as I suspect, the source of the 'hash coercion problem'? and if so, what data type should I make ... possibly should I go all out and make a module with accessors and constructors and the full nine yards, etc.?

Your experience and help is most apprecieted. </CODE>

The actual things I need to keep track of are changing somewhat as I'm becoming aware of what I need ... your answer need not be too specific to the data I have, though it could.

Replies are listed 'Best First'.
Re: Help with ADT
by VSarkiss (Monsignor) on Jul 24, 2002 at 19:51 UTC

    To be honest, I don't understand your data structure. ;-) But from what I can tell, you're mixing arrays and hashes a tad indiscriminately. If nothing else, this won't do what you seem to be expecting: $info->{subdirectory}->{file}->[measurement] = INDEX; If the last item is an array [], then measurement better be an expression that returns an integer.

    However, I think I see an easy way up for you. Get thyself over to CPAN, and look at Class::Struct. It lets you set up nested data structures that may be familiar to you, coming from a C/C++ background.

    HTH ... a little

      Hmm... sounds very interseting. (the module, that is)

      FORGIVE ME, however. IT was just pseudo-code I used in my notes--I should have explained the subdirectory, file, and measurement just mean a valid subdir, file, or measurement. I happen to use variable names in the code: $subdir, $file, and $meas. Sorry for the confusion.

Re: Help with ADT
by DamnDirtyApe (Curate) on Jul 24, 2002 at 19:50 UTC

    A module is probably not a bad idea here, especially if you really need those range constraints (as per your example.) I would recommend taking a look at perldoc perldsc (the data structures cookbook that comes with Perl) for some examples of ADTs.


    _______________
    D a m n D i r t y A p e
    Home Node | Email
Re: Help with ADT
by sauoq (Abbot) on Jul 24, 2002 at 20:20 UTC
    It seems to me that you are storing things in a reference to an array:
    [ ['measurement1', [ ['test subject number', 21, 27], ['value', 50], ... ], ... ], ... ]

    But you are trying to get at the data as if it is a reference to a hash:

    $info->{subdirectory}->{file}->[measurement] = INDEX; $info->{subdirectory}->{file} = PARSER; $info->[measurement]->{name} = NAME;

    But it is hard to tell given the examples you actually gave. Is $info a reference to an array or to a hash?

    Also, you might find your code more readable if you only dereference where you need to do so. For instance, instead of $info->{subdirectory}->{file} try just $info->{subdirectory}{file}.

    By the way, an "ADT" usually means an "Abstract Data Type" not an "Advanced Data Type." One person's advanced data type is another's basic data type.

    I'd be happy to help if you provide a little more information.

    -sauoq
    "My two cents aren't worth a dime.";
    
      Thank you ... don't worry too much over what I have. They were just notes basically, as what I have in my code *has* to change (I knew that basically when I was putting the code in -- I just thought that it might work :)

      I think that the class::Struct is perfect for what I want -- the documentation sure got me exited so far anyway. I'll update this with more information if it doesn't work ... and thank's for all the help.

      Now, as for the "ADT" part ... thanks. One of my classes sometime ago had a book (something like Data Types with C++, or ADTs, or something to that effect). Well, it talked about (beat into the ground) all about ADTs -- it's a shame that I remembered it incorrectly. Yes, I recall now that it is "Abstract"...that reminds me-- Data Abstraction with C++. Decent book, by the way.

Re: Help with ADT
by dragonchild (Archbishop) on Jul 24, 2002 at 21:23 UTC
    Isn't this exactly what objects are supposed to handle? Just make an object, have a bunch of accessors, and store the stuff any way you damn well please. The client only does stuff through the accessors, so you don't have the coercion issues.

    As a note - while you're developing it, DO NOT deal with optimization. You have no idea what to optimize yet. Just use the easiest way to code it, then optimize later. Much, much later.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Objects aren't a magic wand that just make your problems go away. You still have the same potential problems if you are going to use an object. Don't forget, Perl makes you implement the objects yourself. All you have is a single reference, and you have to store all your instance data there. Guess what, then you are facing exactly the same problems! After all, the post is about storing data in a reference.... You still have to write your accessors.

      Abigail

        Yes, you are absolutely correct. I should have been more specific in my post.

        What I was driving at was that he wasn't quite sure how the ADT was going to be specified. In fact, he says that the ADT's definition is constantly changing. This implies, to me, that he's using this thing. By creating some API, he is able to deal with the ADT in some abstract manner within the client. He gets to figure out what exactly he wants this thing's behavior to be.

        Then, and only then, does he have enough information to be able to actually design the darn thing. He's trying to put the cart before the horse.

        For example, I only skimmed the original post, but I'm pretty sure that he doesn't need one ADT, but a composition of ADT's.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Help with ADT
by Ryszard (Priest) on Jul 26, 2002 at 11:26 UTC
    No one has mentioned it yet, but youre having trouble deferrencing your data structure do:
    use Data::Dumper; .. .. print Dumper(<insert datastructre here>);

    Data Dumper is core, so you will have it as part of a vanilla install.

Log In?
Username:
Password:

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

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

    No recent polls found