?
Solved

1 random dbm record

Posted on 2005-02-28
7
Medium Priority
?
299 Views
Last Modified: 2013-12-25
Im using these lines to retrieve records from my database.. It works
fine but I want to display only 1 RANDOM record at a time, which
ofcourse meets all criteria. To be honoust I dont have a clue  how to
display 1 random record at a time..but I'm sure you can help me !?


#!/usr/bin/perl
use CGI;
$q=new CGI;
print "Content-Type: text/html\n\n";

$name = "goods";
die "Missing DBM name.\n" unless $name;
dbmopen(%map,$name,0666);
$rec = 0;
while (($key,$val)=each(%map)) {

($item1,$item2,$item3,$item4) = split("\t",$val);
if ($item2 eq "Radio") {

## check $item3 for "philips"
print qq{$item2<br>};
}}
dbmclose(%map);
exit;
0
Comment
Question by:Skeeter87
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 13418593
while (($key,$val)=each(%map)) { push @vals, $val . "\t" . rand($.); }
@vals=sort @vals;
($item1,$item2,$item3,$item4,$dumm) = split("\t",$vals[0]);
if ($item2 eq "Radio") { print qq{$item2<br>};
0
 

Author Comment

by:Skeeter87
ID: 13418849
Thanks for replying, I've tested it but it doesnt print any record (there are six records in the database)
0
 

Author Comment

by:Skeeter87
ID: 13419445
When I add print @vals; beneath the 'while' line, it prints out the records...
However the split doesnt seem too function ..

Can this method also be used when used with a couple of thousands of records ?
0
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
LVL 51

Expert Comment

by:ahoffmann
ID: 13423777
> doesnt print any record
hmm, can't test you example, just the method
probaly this is a better attempt:

 while (($key,$val)=each(%map)) { push @vals, rand($.) . "\t" . $val; }
@vals=sort @vals;
($dumm,$item1,$item2,$item3,$item4) = split("\t",$vals[0]);

if this fails too, please check if your $item2 contains an expected value


> Can this method also be used when used with a couple of thousands of records ?
it can, but it will be a waste of resources if you just need one of them
I'd use a numeric index in the dbm file and use rand() to select one index randomly, that avoids reading the complete file into memory.
0
 

Author Comment

by:Skeeter87
ID: 13427220
I got it too work !! thanks....

Can you give me an example on how to implentate such an numeric index ?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13427318
as you do not use the key in your example, I'd use that as simple integer
then you have to cast the result of rand() to an integer, that's it ..
0
 

Author Comment

by:Skeeter87
ID: 13429075
ok, thank you very much
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question