For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2006 > sorting DBM hash









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author sorting DBM hash
John W. Burns

2006-09-28, 9:57 pm

Sorting DBM Hash
Greetings:
I've run into what appears to be a conflict in sorting
a DBM Hash. The DBM is opened and closed through tie and untie to store
selections from Perl Tk medical questionnaire which uses checkboxes, radio buttons and
lists, and contains over 200 items. I'm attempting to verify that all user
selections are accurately stored in the DB.
The first sort routine prints out keys and value. However it fails to
include some keys and values such as variable ckb_100 or ckb_104 (or their values).
When I use Randal Schwartz's sort from Learning Perl, it (by design) lists
keys sorted by value order (without listing values), but includes all keys
in the DB including those keys dropped in the first sort. I have reviewed the
PerlTk program a number of times and can't find an error (realize
this doesn't mean there isn't one). But why would Randal's sort contain a
key that is not included in the other sort? Randal's sort indicates all
user selections are included in DB; other sort does not.

Here's the first sort code on the DBM Hash; it fails to list all keys and
their values.
#!/usr/local/bin/perl
use warnings;
use SDBM_File; #load database module which is a clone of DBM provided by ACTIVE STATE
$db = "c:/temp/userstats.db"; #where data is kept between runs
dbmopen(%DATA, $db, 0666 ) or die( "Can't open: $!" );

foreach my $key ( sort { $a <=> $b } keys %DATA ) {
print "$key => $DATA{$key}\n";
}

Here's Randal Schwartz', Learning Perl,sort; it lists all keys.
#!/usr/local/bin/perl
use warnings;
use SDBM_File; #load database module which is a clone of DBM provided by ACTIVE STATE
$db = "c:/temp/userstats.db"; #where data is kept between runs
dbmopen(%DATA, $db, 0666 ) or die( "Can't open: $!" );

my @choices = sort by_score_and_name keys %DATA;
sub by_score_and_name {
$DATA{$b} <=> $DATA{$a} #by descending numeric score
or
$a cmp $b #ASCIIbetically by name
}
print "@choices, \n";
dbmclose(%DATA);

Here's a typical PerlTk GUI:
#Post Traumatic Stress Disorder
$ckb_100 = $frme_name1a -> Checkbutton(-text=>"post traumatic stress
disorder", -variable=>\$post2,
-command =>\&variable_100);
$ckb_100-> deselect();
$ckb_100-> form(-left=>460, -top=>585);

An anonymous sub routine is used to hold sub routines
for all the question variables. Here's a standard sub for
checkbox.
sub variable_100 {
my $x = ${$ckb_100 ->cget(-variable)};
if ($x == 1) {
$DATA{ckb_100} = "1";
}
else {

}

}

Thanks for any insights.
John Burns











nobull67@gmail.com

2006-09-29, 3:57 am


John W. Burns wrote:
> Sorting DBM Hash
> Greetings:
> I've run into what appears to be a conflict in sorting
> a DBM Hash. The DBM is opened and closed through tie and untie to store
> selections from Perl Tk medical questionnaire which uses checkboxes, radio buttons and
> lists, and contains over 200 items. I'm attempting to verify that all user
> selections are accurately stored in the DB.
> The first sort routine prints out keys and value. However it fails to
> include some keys and values such as variable ckb_100 or ckb_104 (or their values).


Are you sure? That is sure they are missing, not just in a different
place in the seqence than you expected?

> foreach my $key ( sort { $a <=> $b } keys %DATA ) {
> print "$key => $DATA{$key}\n";
> }


If the keys of %DATA are strings not numbers then $a <=> $b will return
0 and no sorting will happen. But you shouldn't loose any keys.
You'll get a warning too.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com