Gerald Host wrote:
>
> I'm using DBI's selectall_hashref with 5 key columns.  I want to display
> each row where key col 1 is 'yes' or NULL (undef or '' ?)
>
>
> foreach my $medCategory (keys %{$href->{'yes'}->{qw('' 'yes')}->{qw('' 'yes')}}) { > foreach my $med (keys %{$href->{qw('yes')}->{qw('' 'yes')}->{qw('' 'yes')}->{$medCategory}}) { > foreach my $ID (keys %{$href->{qw('yes')}->{qw('' 'yes')}->{qw('' 'yes')}->{$medCategory}->{$med}}) { > print qq{ $href->{qw('yes')}->{qw('' 'yes')}->{qw('' 'yes')}->{$medCategory}->{$med}->{$ID}->{ID} }; > print qq{ $href->{qw('yes')}->{qw('' 'yes')}->{qw('' 'yes')}->{$medCategory}->{$med}->{$ID}->{Medication} };
>     }
>   }
> }
>
> This isn't working of course because I don't quite understand the syntax I
> need.  Can someone give me a hint?  Thanks!
>
> Ryan

Ryan/Gerald (!)

You don't want to use selectall_hashref because, as is the nature of hashes, the
key must be unique, which means the database table's key field that provides it
must also be unique. If you get this working, you will retrieve a single record
for each possible value of each key: presumably 'yes', 'no', '', and NULL or
something like that.

It does look though as if you're pulling all your database data into a Perl hash
and trying to interrogate that. Surely, in your example above, you should be
writing somthing such as:

  my $data = $dbh->selectall_arrayref(qq(
    SELECT id, medication
    FROM table
    WHERE col1 = 'yes' OR OR col1 = '' OR col1 IS NULL
    ORDER BY id
  ));

  foreach my $row (@$data) {
    printf "%s: %s\n", @$row;
  }

(I don't know what the 'yes' field's name is, or what the table name is.)

By the way, qw doesn't work like that - you've used it as a tool that just
allows you to miss out the commas.

  qw(yes no maybe)

is the same as

  ('yes', 'no', 'maybe')

i.e. its effect is the same as a call to split() on the string within the
delimiters. There's no way no write the empty string using this of course, so
your

  qw('' 'yes')

should be

  ('', 'yes')

but that won't make the program work!

Hope this helps,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to