ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> visualperl-discuss
visualperl-discuss
RE: [Visualperl-discuss] Help with DBI pointer
by Orton, Yves other posts by this author
Aug 7 2002 5:13PM messages near this date
view in the new Beta List Site
[Visualperl-discuss] 'VPM Browser' Option in VS.Net | [Visualperl-discuss] Help please
>  I don't want to have to run the query (&getTrailmapData) 
>  everytime to get
>  the db pointer and retrieve a different column of data.  I 
>  just want to have
>  the $pointer set to the beginning of the DB so I can get 
>  another column of
>  data.

Hmm. well there are a few approaches to your problem. First may I point out
that your habit using the & sigil in front of subroutine calls will
eventually bite you in the heiny.

consider:
----------------------
use Carp qw(confess);

sub foo {
  confess "Got @_ as args"  if @_;
  print "No foo here!\n";
}

sub bar {
  &foo();
  &foo;
}

bar(1,2,3);
__END__
No foo here!
Got 1 2 3 as args at C:\Temp\confess.pl line 4
	main::foo called at C:\Temp\confess.pl line 10
	main::bar(1, 2, 3) called at C:\Temp\confess.pl line 13
----------------------
Now I know why that happened, do you? And more to the point will the newbie
perl programmer who is assigned to maintain the code? :-)

Also, it looks to me like $sth is a global in your script.  Another way to
bitten. :-)
And to make it even worse your code looks to me like it doesnt use
strictures or CGI.  Both of these are really good ways to write buggy
scripts and produce invalid HTML.

Anyway, your problem is that DBI _isnt_ mainting an in memory copy of the
result of your query. It doesnt do this normally because the chance it would
overflow the available ram.  Thus you either need to look at 

$sth-> fetchall_hashref();

Which will do this (and then use it at your own risk) or make your code
smarter about how it does its business (my preference). My suggestions do
not incorporate CGI (although they should) but they are strict safe.

  	&getTrialmapData();
     	$sth-> {ChopBlanks}=1;
      my %ColHash;
      $ColHash{sampleID} ="<tr> <td width = '18%'>Sample ID #</td>";
      $ColHash{substrate}="<tr> <td width = '18%'>Substrate</td>";
     	while ($pointer = $sth-> fetchrow_hashref)
     	{
            foreach my $col (qw(sampleID substrate)) {
			$ColHash{$col}.="<td> $pointer->{$col}</td>";
            }
     	}
      print $ColHash{$_}."</tr> \n" foreach qw(sampleID substrate);

Should do the trick.  (although it is untested)   	

HTH

Yves
ps (I think you may benefit from doing:

	use Data::Dumper;
      ...
   	while ($pointer = $sth-> fetchrow_hashref)
     	{
         print Dumper($pointer);
     	}

Also one last bitch, but calling a REFERENCE a POINTER is probably going to
confuse you and other people quite a bit (although it is true that perls
refs are more like C's pointer than they are like c's refs, but thats
another matter entirely)
)

     	
 
Attachments:
unknown1


Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved