For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > April 2005 > Looping (continued)









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 Looping (continued)
Nikos

2005-04-23, 8:56 am

Hello i still cant make this work:

$st = $db->prepare( "SELECT * FROM counter" );
$st->execute();

my @tableRows;
while ( my $row = $st->fetchrow_hashref() ) {
push @tableRows, $row;
}

print table( {class=>'info'},
map {
Tr(
td( submit( -name=>'game', -value=>$_->{name} )),
td( $_->{text} ),
td( $_->{name} )
)
} @tableRows
);

I only got 1 row with 3 fileds one having a button saying game and
beside it 2 empty cells instead of the big game list i was producing
with the print statements before i turn it to loop.
Nikos

2005-04-23, 8:56 am

Nikos wrote:
Please can someone make this work?

#!/usr/bin/perl -w
use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use DBI;
use DBD::mysql;

my @months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec');
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime;
my $xronos = "$mday $months[$mon], $hour:$min";

my $game = param('game');

my $ip = $ENV{'REMOTE_ADDR'};
my @numbers = split (/\./,$ip);
my $address = pack ("C4", @numbers);
my $host = gethostbyaddr ($address, 2) || $ip;

print header( -charset=>'iso-8859-7' );
print start_html( -style=>'../style.css', -title=>'Ðáé÷_éäÜêéá êáé ü÷é
ìü_ï!', -background=>'../data/images/night.gif' );


my $db = ($ENV{'SERVER_NAME'} ne 'nikolas.50free.net')
? DBI->connect('DBI:mysql:nikos_db', 'root', '')
: DBI->connect('DBI:mysql:nikos_db:50free.net', 'nikos_db',
'tiabhp2r')
or print font({-size=>5, -color=>'Lime'}, $DBI::errstr) and exit 0;

print p( font( {-size=>4, -color=>'Lime'}, "Áðü åäþ ìðïñåßò _á
êáôåâÜóåéò ðáé÷_ßäéá üðùò åðßóçò êáé äéêÜ ìïõ ðñïãñÜììáôá:<br>" ),
font( {-size=>4, -color=>'Lime'}, "ÃñÜøå ìïõ
ôéò å_ôõðþóåéò óïõ óôï "),
font( {-size=>4, -color=>'White'},
"nik0s\@mycosmos.gr<br><br>" ));

print start_form(-action=>'games.pl');

my $st = $db->prepare( "SELECT * FROM counter" );
$st->execute();

my @tableRows;
while ( my $row = $st->fetchrow_hashref() ) {
push @tableRows, $row;
}

print table( {class=>'info'},
map {
Tr(
td( submit( -name=>'game', -value=>$_->{name} )),
td( $_->{text} ),
td( $_->{name} )
)
} @tableRows
);

print '<br>';

if ( !param() ) { print p( a( {href=>'index.pl'}, img
{src=>'../data/images/back.gif'} )); }


if ( param() )
{
$db->do( "UPDATE counter SET $game = $game + 1" );
$st = $db->prepare( "SELECT $game FROM counter" );
$st->execute();
$row = $st->fetchrow_hashref;

print p( font( {-size=>4, -color=>'Yellow'}, "Åßóáé ï " ),
font( {-size=>4, -color=>'White'},
"$row->{$game}" ),
font( {-size=>4, -color=>'Yellow'},
"ïò ðïõ êáôåâÜæåé ôï " ),
font( {-size=>4, -color=>'White'},
"$game!<br>" ),
font( {-size=>4, -color=>'Yellow'},
"Åëðßæù _á óïõ áñÝóåé êáé _á óïõ öá_åß ÷ñÞóéìï!" ));

$db->do( "UPDATE logs SET keimeno='$game' WHERE host='$host'" ) or
die $db->errstr;

print p( a( {href=>'index.pl'}, img
{src=>'../data/images/back.gif'} ));
print "<script
language='Javascript'>location.href='../data/games/$game.rar'</script>";
}

Iam getting thsi when i try to run it:

Global symbol "$sec" requires explicit package name at D:\www\cgi-bin\
+games.pl line 9.
Global symbol "$min" requires explicit package name at D:\www\cgi-bin\
+games.pl line 9.
Global symbol "$hour" requires explicit package name at D:\www\cgi-bin
+\games.pl line 9.
Global symbol "$mday" requires explicit package name at D:\www\cgi-bin
+\games.pl line 9.
Global symbol "$mon" requires explicit package name at D:\www\cgi-bin\
+games.pl line 9.
Global symbol "$year" requires explicit package name at D:\www\cgi-bin
+\games.pl line 9.
Global symbol "$wday" requires explicit package name at D:\www\cgi-bin
+\games.pl line 9.
Global symbol "$yday" requires explicit package name at D:\www\cgi-bin
+\games.pl line 9.
Global symbol "$isdst" requires explicit package name at D:\www\cgi-bi
+n\games.pl line 9.
Global symbol "$mday" requires explicit package name at D:\www\cgi-bin
+\games.pl line 10.
Global symbol "$mon" requires explicit package name at D:\www\cgi-bin\
+games.pl line 10.
Global symbol "$hour" requires explicit package name at D:\www\cgi-bin
+\games.pl line 10.
Global symbol "$min" requires explicit package name at D:\www\cgi-bin\
+games.pl line 10.
Global symbol "$row" requires explicit package name at D:\www\cgi-bin\
+games.pl line 62.
Global symbol "$row" requires explicit package name at D:\www\cgi-bin\
+games.pl line 65.
Execution of D:\www\cgi-bin\games.pl aborted due to compilation errors
Mark Clements

2005-04-23, 8:56 am

Nikos wrote:
> Hello i still cant make this work:
>
> $st = $db->prepare( "SELECT * FROM counter" );
> $st->execute();
>
> my @tableRows;
> while ( my $row = $st->fetchrow_hashref() ) {
> push @tableRows, $row;
> }
>
> print table( {class=>'info'},
> map {
> Tr(
> td( submit( -name=>'game', -value=>$_->{name} )),
> td( $_->{text} ),
> td( $_->{name} )
> )
> } @tableRows
> );
>
> I only got 1 row with 3 fileds one having a button saying game and
> beside it 2 empty cells instead of the big game list i was producing
> with the print statements before i turn it to loop.

I've said this before: it looks like I will have to say it again. *What*
is in @tableRows following the while loop that populates it? As I have
also said before, you can use Data::Dumper to examine the contents of
data structures.

use Data::Dumper;

.....

warn Dumper \@tableRows;

will dump the contents of @tableRows to the console if you run the
script from the command line.

Sigh.

Mark
Mark Clements

2005-04-23, 8:56 am

Nikos wrote:
> Nikos wrote:
> Please can someone make this work?

<snip>

No. We don't have access to your environment. You have to learn to debug
your own programs.

> ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime;

<snip>

> Global symbol "$sec" requires explicit package name at D:\www\cgi-bin\
> +games.pl line 9.

<snip error messages on this theme>
As the documentation for strict

perldoc strict

states, your variables need to be declared before use, typically with my.

Is any of this getting through?

Mark
Tad McClellan

2005-04-23, 3:57 pm

Nikos <hackeras@gmail.com> wrote:

> Please can someone make this work?



Please post your job offers in a newsgroup for job offers, or
advertise on the perl-jobs mailing list.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Joe Smith

2005-04-25, 3:58 am

Nikos wrote:

> my @months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
> 'Sep', 'Oct', 'Nov', 'Dec');


It appears that you are using an out-of-date book for a reference.

my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);

> ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime;


The fix for that line is to simply add two characters to it.

-Joe
Sponsored Links







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

Copyright 2008 codecomments.com