The project I am working on requires a Berkely DB even though I prefer DBI/MySQL/Oracle or even DBI/CSV solutions. I need to limit the the data returned two to rows even if more match the query - Using a DBI/SQL solution I would just state: LIMIT = 2. Any ideas as to how to do this in Perl 5 (RH Linux 6.2) would be much appreciated - I'm not all that great with the "sort" function. some ugly sample code:
my ($cur_day, $cur_mon, $cur_yr) = (localtime())[3,4,5]; $cur_mon++; $cur_yr+=1900; my $cur_date=sprintf("%04d%02d%02d", $cur_yr, $cur_mon, $cur_day); my $limit = 2; do { foreach my $event_id (sort { $Sorts{$sortBy}->() } keys (%database) +) { my ($event_date_1, $event_title_1, $venue, $series, $style) = split(/\0/, $database{$event_id}); if ($event_date_1 >= $cur_date) { &getRandFeature; &Generate_Parsed_File(); } } } until ( $limit ); # code in debug mode "test2.pl" 180 lines, 4953 characters print "rand_int =" . "$rand_int\n"; print "rand_int2 =" . "$rand_int2\n"; my @index_db = %fdatabase; my @index_db2 = %database; # if ($index_db2[$rand_int2] > $limit) { # return(2); # } # else { print "array element:" . "$index_db2[$rand_int2]\n"; print "array element:" . "$index_db[$rand_int]\n"; # } } sub Generate_Parsed_File { # my $tmplFName = shift; # my $outFName = shift; my $Parameters = shift; open (OUT_FILE, "< $tmplFName") or die ("WARNING: Could not locate '$tmplFName': $!\n"); $_ = join('', <OUT_FILE>); s/<!-- ::(.+?):: -->/${$Parameters}{$1}/gs; s/::(.+?)::/${$Parameters}{$1}/gs; open (NEW_FILE, "> $outFName") or die ("WARNING: Could not locate '$outFName': $!\n"); print NEW_FILE $_; close (OUT_FILE); close (NEW_FILE); }
Thanks in advance - I'm no computer scientist, perl is my first programming language and up to a year ago I only wrote shell scripts.. sean

In reply to berkely database fun by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.