devslashneil has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I've recently written a CGI script which queries a database and displays information about a user, however i now want to be able to only display columns in a table which contain a value other than "NA".

I wrote some code to do this, however it doesn't seem to be deselecting columns that contain all rows with the value "NA".

Also this code seems really bloated and bad.
Any help would be much appreciated.
Here is the code:
#!/usr/bin/perl use DBI; my $listname = shift; my @fieldlist = qw/S_FName S_SName S_Address S_Postcode S_State S_Coun +try S_Email S_Company/; my $field = (); foreach $field (grep(not_NA($listname,$_), @fieldlist)){ print "<td ALIGN=Left>".$field . "</td>"; } sub not_NA { my $listname = shift; my $field = shift; chomp $field,$listname; my $mid = lookup_MID($listname); my $db = "nMail"; my $sock = "/tmp/mysql.sock"; my $user = "user"; my $pass = 'password'; my $dsn = "DBI:mysql:$db;mysql_socket=$sock"; my $dbh = DBI->connect($dsn,$user,$pass); my $sth = $dbh->prepare("SELECT $field from ML_Subscribers WHE +RE MID = $mid"); $sth->execute() || die "Error: Could not get mailing list data +\n"; while (my @row = $sth->fetchrow_array) { if($row ne 'NA'){ $sth->finish(); $dbh->disconnect(); return 1; } } $dbh->disconnect(); return 0; } sub lookup_MID { my $listname = shift; chomp $listname; my $db = "nMail"; my $sock = "/tmp/mysql.sock"; my $user = "user"; my $pass = 'password'; my $dsn = "DBI:mysql:$db;mysql_socket=$sock"; my $mid = (); my $dbh = DBI->connect($dsn,$user,$pass); my $sth = $dbh->prepare("SELECT MID,Mname FROM ML_Lists"); $sth->execute() || die "Error: Could not get mailing list data +\n"; while (my @row = $sth->fetchrow_array) { chomp $row[0],$row[1]; if($row[1] eq $listname){ $mid = $row[0]; } } $dbh->disconnect(); if(!$mid){ return 0; } return $mid; }
Thanks for your input.

Neil Archibald
- /dev/IT -

Replies are listed 'Best First'.
Re: CGI Table Column Selection
by archon (Monk) on Jul 09, 2003 at 03:51 UTC
    while (my @row = $sth->fetchrow_array) { if($row ne 'NA'){

    this is just plain wrong. @row is an array. there is no $row. why aren't you using strict and warnings? this shouldn't even compile.

    you could make the database do the work and add a bunch of "where field != "NA" to your query. otherwise, you'll have to parse every field returned somehow.

      Eek, thank you for that, i completly missed that, i meant $row[0].
      I am using strict and warnings in my actual code, this was just a small section of my code cut and paste into a new file to see if it works.
      Thanks for your input though although i didn't mean i wanted the data from the database, i simply wanted to know the names of the fields which contain a value other than "NA".
      Sorry if i phrased my question incorrectly.

      Neil Archibald
      - /dev/IT -
Re: CGI Table Column Selection
by nysus (Parson) on Jul 09, 2003 at 06:37 UTC
    This doesn't address your question but the following line

    chomp $field,$listname;

    does not do a chomp on both scalars. It only chomps the first and then throws away the second. Instead, you want to put the scalars into a list context, like so: chomp ($field,$listname);

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff";
    $nysus = $PM . $MCF;
    Click here if you love Perl Monks

      Wow, i never thought about it, but looking at it now, it seems so obvious, thanks a lot :-)

      Neil Archibald
      - /dev/IT -
        No problem. I also just noticed that you are not using strict or warnings. Always, always, always, use strict and warnings. Just throw 'use strict' and 'use warnings' in at the top of your code.

        One more thing, the most common way to pass arguments into a subroutine is my ($scalar1, $scalar2) = @_;. You used two successive shift calls and if you coninued this practice over the course of a couple of lifetimes, it could lead to a crippling repetitive stress disease. But most importantly, it just makes your code slightly more readable.

        Update: Ignore my admonition about strict and warnings. I see your previous post.

        $PM = "Perl Monk's";
        $MCF = "Most Clueless Friar Abbot Bishop Pontiff";
        $nysus = $PM . $MCF;
        Click here if you love Perl Monks

(jeffa) Re: CGI Table Column Selection
by jeffa (Bishop) on Jul 09, 2003 at 14:48 UTC
    Connecting and disconnecting to the same database in the same script is silly ... you could go to the trouble of creating a Singleton, or you could simply connect to the database at the beginning and use $dbh as a global variable (some globals are good). Also, you can cut back on having to worry about database errors if you turn on RaiseError:
    my $dbh = DBI->connect($dsn,$user,$pass,{RaiseError=>1});
    Now DBI will die with $DBI::errstr for you.

    Onward ... so, what you want is to select the rows whose fields ALL do not contain the token 'NA'. You can do this with SQL, if you don't mind a lot of appended comparisons:

    SELECT MID,Mname FROM ML_Lists WHERE MID != 'NA' OR ML_Lists != 'NA'
    There, no Perl code needed. But, since you asked ... here is one way to do it:
    my @list; my $sth = $dbh->selectall_arrayref('select MID,Mname from ML_Lists'); for (@$sth) { push @list,$_ if grep(/^NA$/,@$_) != @$_; }
    grep is used to find the number of fields that contain only the string 'NA' and that number is compared to the total number of rows (@$_ in this context) to see if they are the same. If they are not, then the row contains at least one field that does not have the value 'NA', and that row is pushed to @list. (Personally, i'd rather let the database handle this job, not Perl.)

    Hope this helps, and if you haven't already, then please check out The fine art of database programming. Purchasing Programming the Perl DBI isn't a bad idea either (considering you read it, of course). Hope this helps. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)