in reply to Storing Info with Accessors

graq asked:

If I create a key,value pair in a hash(ref) which is stored in an array(ref) which is one of the methods on the object, will I need to rebuild the array and reset the property in order to effect the change?

Without actually running your code, everything appears to be correct. As for your question, it appears to be a bit vague. What do you mean 'create a key/value' pair? Are you referring to one of the pairs creating in the BEGIN block? Are you referring to updating a key/value pair for a particular supplier or or adding a new key/value pair on the fly? None of these should be particularly difficult.

In reading through your code, I'm not sure why you have the arrayref of hashrefs. It appears to me that this could be slow if you have many suppliers. The data structure that I am seeing looks like this (just the structure, I don't know the keys):

[ { 'supplier_id' => 17, 'item_foo' => 3, 'item_bar' => 7 }, { 'supplier_id' => 28, 'item_foo' => 5, 'item_qux' => 202 } ]

Unless the array index actually conveys useful information, I would probably turn this into a hashref of hashrefs with the primary hashs keys being the supplier_id:

{ '28' => { 'item_foo' => 5, 'item_qux' => 202 }, '17' => { 'item_foo' => 3, 'item_bar' => 7 } };

If this works for you, your &set_stock sub becomes simpler and faster:

sub set_stock { my( $self, $supplier, $line, $stockLevel ) = @_; my $supplierList = (); my $supplierNumber = $supplier->supplier_id; # $supplierList will be a reference to a hash of references to has +hes. unless( $supplierList = $self->get('supplier_list') ) { die( "Cannot set stock level when supplier list is corrupt: $s +upplierList" ); } my $reference = $line->reference; $supp->{ $supplierNumber }{ $reference } = $stockLevel; }

Now that I've gotten thoroughly off-topic, what was your question again? :) If you can provide more detail of what problem you're trying to solve...

Cheers,
Ovid

Vote for paco!

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: (Ovid) Re: Storing Info with Accessors
by graq (Curate) on Aug 02, 2001 at 12:10 UTC
    What do you mean 'create a key/value' pair?

    I am refering to the hash for the new item=>stockLevel for a supplier.

    I would probably turn this into a hashref of hashrefs with the primary hashs keys being the supplier_id

    I think that is a good idea too, thanks :)

    Now that I've gotten thoroughly off-topic, what was your question again?

    By no means! You have hit the nail on the head.

    If you can provide more detail of what problem you're trying to solve...

    Hmm :)
    The object is an order. Each order has a set of suppliers that can supply at least one of the items in the order. Each supplier should have a complete list of how many of each of the items in the order they have in stock.

    Statement: Using the data structure and sub in your reply (assuming the syntax is correct), calling &set_stock will effect a permament change in the object properties without the need for calling the &set function.

    My question (pretty much) was "Is the above statement correct?"

    --
    Graq