Hello Monks, my following code here shows a circular ring buffer simulation that can either be written to or read from using the subs "store" and "retrieve", respectively. I am attempting to teach myself advanced data structures and I lack access to examples so I make my own cases. The ring buffer has a head and a tail to be used for checking the buffer emptiness or fullness.
As long as the head does not "meet" the tail the buffer is not empty and still has data that can be read from, this is technically explained as ($tail != $head), correct?, And to tell whether a buffer is full we do check "
$buffer[$tail]{next} !=$head". I failed to put this second case in plain English that is void of technicality for me to understand it better (My background is not entirely IT), I even studied this article from
Wikipedia, but yet failed, can anyone do it by drawing my attention to a mnemonic parallel or a correlation between these two conditions or something please?.
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?!
Excellence is an Endeavor of Persistence.
Chance Favors a Prepared Mind.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.