biohisham has asked for the wisdom of the Perl Monks concerning the following question:
My other issue is that, I could not figure out a nice neater way to prepare the data structure to hold the indices, so I need to learn from you how to create this data structure in a more efficient manner. Like, I don't want to have to manually fill the hash keys or fill the array with hashes an index at a time and a key at a time. If possible, how can I automate the array holding the hash so I can give it incrementing values to fill each "next" key without an intervention on my part?,I have tried for the past 2 hours but none of the loops I came up with worked so I'm dry from tricks and open to learn new ones from you :), Here is my code everyone:
use strict; my @buffer=( { data=>'', next=>1 },{ data=>'', next=>2, },{ data=>'', next=>3 }, { data=>'', next=>4 } ); sub store { if($buffer[$tail]{next} !=$head){ $buffer[$tail]{data}=shift; $tail=$buffer[$tail]{next}; return 1 }else{ return "Buffer is full"; } } sub retrieve{ if($tail != $head){ my $data=$buffer[$head]{data}; $head=$buffer[$head]{next}; return $data; }else{ return "Empty\n"; } } store "20"; print retrieve;
Eventually, what are the potential uses of such code, using arrays of hashes in buffers simulation is an example I can think of but what other similar examples of applications there are that such an array of hashes structure use can really save me for the day?!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Linked lists in an array of hashes
by ikegami (Patriarch) on Oct 20, 2009 at 19:47 UTC | |
|
Re: Linked lists in an array of hashes
by GrandFather (Saint) on Oct 20, 2009 at 20:54 UTC | |
|
Re: Linked lists in an array of hashes
by pemungkah (Priest) on Oct 20, 2009 at 21:32 UTC | |
|
Re: Linked lists in an array of hashes
by gmargo (Hermit) on Oct 20, 2009 at 19:31 UTC | |
|
Re: Linked lists in an array of hashes
by zwon (Abbot) on Oct 20, 2009 at 19:35 UTC |