John W. Burns am Donnerstag, 28. September 2006 17:11:
> Sorting DBM Hash
> Greetings:
Hello John W.
> 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 such as variable ckb_100 or ckb_104 (or their values) along with
> a few other variables.
> 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 strict;
> 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 ) {
Try to replace the numerical comparison '<=>' with the alphanumerical 'cmp'
one, since your keys are alphanumerical.
Didn't you get a warning of the sort
"Argument "ckb_100" isn't numeric in sort at -e line x."?
> 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
^
hint
> }
> 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 {
>
> }
>
> }
Dani
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>