Looping almost the same repetitive lines

N

Nikos

print table( {class=>'info'},
Tr( td( submit( -name=>'game', -value=>'LogReader' )),
td( "ÄéáâÜóôå Üíåôá êáé åõ÷Üñéóôá ôá Log óáò! Made by Skia ;-)" ),
td( $row->{LogReader} )),
Tr( td( submit( -name=>'game', -value=>'Hangman' )),
td( "Èõìçèåßôå ôá ðáéäéêÜ óáò ÷ñüíéá! Ãáßîôå êñåìÜëá!" ),
td( $row->{Hangman} )),
Tr( td( submit( -name=>'game', -value=>'Penguin' )),
td( "Åëåõèåñþóôå ôïõò öõëáêéóìÃíïõò ðéãêïõßíïõò áðü ôïõò ðÜãïõò!" ),
td( $row->{Penguin} )),
Tr( td( submit( -name=>'game', -value=>'ChickenInvaders' )),
td( "Ç åðßèåóç ôçò âáëôï÷éïíüêïôáò ôïõ Ricudi! Öõëá÷ôåßôå!" ),
td( $row->{ChickenInvaders} )),
Tr( td( submit( -name=>'game', -value=>'DxBall2' )),
td( "Ôï êáëýôåñï Arkanoid ðïõ Ã÷ù äåé ðïôà ìïõ!" ),
td( $row->{DxBall2} )),
Tr( td( submit( -name=>'game', -value=>'JezzBall' )),
td( "¼óïé áãáðÞóáôå ôï Xonix èá áãáðÞóåôå óßãïõñá êáé áõôü!" ),
td( $row->{JezzBall} )),
Tr( td( submit( -name=>'game', -value=>'Mahjongg' )),
td( "Ôï ãíùóôü óå üëïõò Shangai êáé Üëëá êéíÃæéêá ðáé÷íßäéá!" ),
td( $row->{Mahjongg} )),
Tr( td( submit( -name=>'game', -value=>'FeedingFrenzy' )),
td( "Ôáßóôå ôï øáñÜêé Frenzy Ãùò üôïõ ãßíåé ìåãÜëï êáé äõíáôü!" ),
td( $row->{FeedingFrenzy} )),
Tr( td( submit( -name=>'game', -value=>'Astrobatics' )),
td( "ÃåñéçãåéèÞôå óôï ÄéÜóôçìá êáé åîïíôþóôå üëá ôá åìðüäéá!" ),
td( $row->{Astrobatics} )),
Tr( td( submit( -name=>'game', -value=>'Scrabble' )),
td( "Ãïéüò åßíáé ãéá Ãíá ðáé÷íßäé Scrabble? Ôï ðëÃïí äçìïöéëÃò!" ),
td( $row->{Scrabble} )),
Tr( td( submit( -name=>'game', -value=>'PacMania2' )),
td( "Äýóêïëï Pacman ãéá ìáíéáêüõò ôïõ åßäïõò! Äåí èá îåêïëëÜôå!" ),
td( $row->{PacMania2} )),
Tr( td( submit( -name=>'game', -value=>'FrozenBubble' )),
td( "ÃáãùìÃíï Bubble Puzzle ìå åîáéñåôéêÞ ìïõóéêÞ!" ),
td( $row->{FrozenBubble} )),
Tr( td( submit( -name=>'game', -value=>'LinkLines' )),
td( "Ãêüìá Ãíá êëáóéêü ðáé÷íßäé ìå ãñáììÃò êáé ìðÜëåò!" ),
td( $row->{LinkLines} )),
Tr( td( submit( -name=>'game', -value=>'ComboChaos' )),
td( "Ãáé÷íßäé ðáñáôçñçôéêüôçôáò! Ãüóï ãñÞãïñá âñßóêåéò ôá
üìïéá?!?" ),
td( $row->{ComboChaos} )),
Tr( td( submit( -name=>'game', -value=>'MadCaps' )),
td( "ÔñåëïêáðÜêéá! ÂÜëôå ôá óå ìéá ôÜîç åðéôÃëïõò!" ),
td( $row->{MadCaps} )),
Tr( td( submit( -name=>'game', -value=>'MiniGolf' )),
td( "ÔñéóäéÜóôáôï Minigolf! Óõíáãùíçóôåßôå ôïõò ößëïõò óáò!" ),
td( $row->{MiniGolf} )),
Tr( td( submit( -name=>'game', -value=>'AstroPop' )),
td( "Åôïéìáóôåßôå íá ðáßîåôå Ãíá ðñùôüôõðï äéáóôçìéêü ðáé÷íßäé!" ),
td( $row->{AstroPop} )),
Tr( td( submit( -name=>'game', -value=>'TinyCars2' )),
td( "Ããþíåò äñüìïõ ìå ëéëéðïýôåéá áìáîÜêéá!" ),
td( $row->{TinyCars2} )),
Tr( td( submit( -name=>'game', -value=>'BrainsBreaker' )),
td( "Åîáóêçèåßôå óôçí õðïìïíÞ! ÖôéÜ÷ôå óõíáñðáóôéêÜ Puzzles!" ),
td( $row->{BrainsBreaker} )),
Tr( td( submit( -name=>'game', -value=>'AlphaBall' )),
td( "ÔñéóäéÜóôáôï Arkanoid ðïõ îå÷ùñßæåé áðü ôá õðüëïéðá ôïõ
åßäïõò ôïõ!" ),
td( $row->{AlphaBall} )),
Tr( td( submit( -name=>'game', -value=>'VortiBall' )),
td( "ÂïçèÞóôå áõôÞ ôç ìðÜëá íá âñåß ôï äñüìï ôçò!" ),
td( $row->{VortiBall} )),
Tr( td( submit( -name=>'game', -value=>'Tetris4000' )),
td( "Ãðïëáõóôéêü ôñéóäéÜóôáôï Tetris ãéá ôïõò öáíáôéêïýò ôïõ
åßäïõò!" ),
td( $row->{Tetris4000} )));
print end_form();

Is there a way to write the above code with the use of a loop?
Each line is slightly different from the other i dont know how to lopp it.

The above code was much-much worse before i udes CSS in "style.css" like
the following:

table.info {
background-image : url(../data/images/nebula.jpg);
width : 25%;
color : yellow;

text-align : center;
font : 18px times;

border : 3px ridge red;
border-collapse: : collapse;
}

it was all print and font and width and align and stuff like that that i
hate but now with the use of css i can just describe it just once.

If you see something wrong in it or can suggest somethign better please
let me know.
 
M

Mark Clements

Nikos said:
print table( {class=>'info'},
Tr( td( submit( -name=>'game', -value=>'LogReader' )),
td( "ÄéáâÜóôå Üíåôá êáé åõ÷Üñéóôá ôá Log óáò! Made by Skia ;-)" ),
td( $row->{LogReader} )),
Tr( td( submit( -name=>'game', -value=>'Hangman' )),
td( "Èõìçèåßôå ôá ðáéäéêÜ óáò ÷ñüíéá! Ãáßîôå êñåìÜëá!" ),
td( $row->{Hangman} )),
Is there a way to write the above code with the use of a loop?
Each line is slightly different from the other i dont know how to lopp it.
HTML::Template or similar would be good for this, but then this has been
pointed out to you before. If you insist on doing it this way: load the
data from an external file or (better) a database;

Assuming each row of data is loaded into a hashref, something like this
should do it:

my @tableRows = ();
while(my $item = getDataRowByWhateverMeans()){
push @tableRows, $item;
};

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

You need to think about separation of data and code as well as
separation of logic and content.

Mark
 
N

Nikos

Mark Clements wrote:

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

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

@tableRows = ();

while( $item = $row ) {
push @tableRows, $item;
};

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

print end_form();

Although i have created the hashref when i try to run games.pl the page
never loads.
I cant see the mistake.

ps. its ok about the my i actually never use them.
 
M

Mark Clements

Nikos said:
Mark Clements wrote:

print start_form(-action=>'games.pl');
I didn't, but anyway...
$st = $db->prepare( "SELECT * FROM counter" );
$st->execute();
$row = $st->fetchrow_hashref;

@tableRows = ();

while( $item = $row ) {
push @tableRows, $item;
};
You misunderstand DBI.

Are you sure that $db is a valid db handle? You need to check the return
values of both prepare and execute.

Then you need to do

while(my $item = $st->fetchrow_hashref()){
blah blah

Although i have created the hashref when i try to run games.pl the page
never loads.
I cant see the mistake.
What happens when you print the results to the screen ie run the script
from the command line?
ps. its ok about the my i actually never use them.
This is a mistake. You *need* to run with

use warnings;
use strict;

as these catch a heap of errors for you. You need to scope variables as
tightly as possible. This involves using my. If you don't take this
simple advice, then you will be causing yourself a lot of grief, and
when you turn to this newsgroup for advice, then people will not be
very willing to help you. In fact, the first thing they will say will be
why aren't you.....

Also, learn to use Data::Dumper: it is very useful for inspecting data
structures.

Mark
 
N

Nikos

Mark said:
Are you sure that $db is a valid db handle? You need to check the return
values of both prepare and execute.

Then you need to do

while(my $item = $st->fetchrow_hashref()){
blah blah



What happens when you print the results to the screen ie run the script
from the command line?

Yes $db is ok since i assign the value to it:

$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', '*****')
or print font({-size=>5, -color=>'Lime'}, $DBI::errstr) and exit 0;


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

@tableRows = ();

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

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

Whne i perl -c games.pl it says games.pl syntax OK from command line

when i view it from the broswer the only thing it displays is some other
text above that code and nothing more. i t not evne designs the table.
 
N

Nikos

But man i write small cgi progs and i always define the varibles and use
them once so its not big deal if i dont use my.
 
M

Mark Clements

Nikos said:
$st = $db->prepare( "SELECT * FROM counter" );
you need to check the return value of this.

What happens when you execute this statement directly within the db
console? (mysql in this case) Do you get any results?
$st->execute();
and checj the return value of this.
$row = $st->fetchrow_hashref;

@tableRows = ();

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

What happens when you use Data::Dumper to display @tablerows? Is it
empty? If so, you have an issue with your query.


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

Whne i perl -c games.pl it says games.pl syntax OK from command line
Fine: it compiles. That doesn't tell us a lot. What happens when you
*run* it?
when i view it from the broswer the only thing it displays is some other
text above that code and nothing more. i t not evne designs the table.
You need to learn to partition your problem. What the browser displays
is academic at this point; first of all you need to ascertain what the
script is doing at what it outputs. You don't seem to know how to debug
your programs.

You *need* to use strict. You *need* to use warnings. You *need* to use
my. If you don't understand why, you need to read some more.

Mark
 
M

Mark Clements

Nikos said:
But man i write small cgi progs and i always define the varibles and use
them once so its not big deal if i dont use my.

Nikos: you can drive a car without ever using the mirror, but that
doesn't make it a good idea. You can also drive a car without using the
clutch and gearstick (ok - or the equivalent in an automatic car), but
you won't get very far. You've been advised *multiple* times to use
warnings, and strict, and lexically scoped variables. There is good
reason for that advice. You have already exhausted the patience of one
person here who has tried to help you. If you want help, you have to
take certain things on board. You do not appear to be doing this.

I don't think I can help you any further.

regards,

Mark
 
T

Tad McClellan

Nikos said:
But man i


It is not about *you*.

It is about your audience (us).

Rather than use strict and have a computer find common mistakes,
you ask *hundreds of live people* to find the common mistakes.

You are disrespectful of people's time when you ask them to
debug before asking a machine to debug.

It is demeaning to be asked to do the work of a machine.

If you continue to demean folks here, pretty soon you will be
in all of the killfiles, and getting help will be much much harder.

Do whatever you like in *your* programs, but if you are going to
ask volunteers to help with it, then first put

use strict;
use warnings;

at the top of your program and get it to run without complaint.

write small cgi progs and i always define the varibles and use
them once so its not big deal if i dont use my.


Every college CS freshman knows that global variables are bad.

But you seem to know better than the collective wisdom of
nearly the entire history of computing?

I'm sceptical...
 
T

Tad McClellan

You've been advised *multiple* times to use
warnings, and strict, and lexically scoped variables. There is good
reason for that advice.


But Nikos knows better than us, and so can reject common wisdom
that all of us "lesser" people follow as a matter of best practices.

(which makes me wonder why he needs our help...)

If you want help, you have to
take certain things on board. You do not appear to be doing this.

I don't think I can help you any further.


Me either, which is why I did what I did. It is hopeless.
 
N

Nikos

Tad said:
Do whatever you like in *your* programs, but if you are going to
ask volunteers to help with it, then first put

use strict;
use warnings;

at the top of your program and get it to run without complaint.

OK. I will put those 2 lines in front of every script of mine.
Actually it was the *use script* i didnt so far working with cause i had
the use warning not as use but in the very first statement of
#!/usr/bin/perl -w so i was actually using it :)

but *i will* use sctrict as well.

I have no intension to be disrespectfull or estimate myself higher than
all of you by any means. After all its *I* that ask questions i cant
make the simpelst program work.

Please dont consider it this way.
Its just that if i dont understand exactly why i am using something i
dont use because it confuses me.

But i will do as i been told.

Every college CS freshman knows that global variables are bad.

But you seem to know better than the collective wisdom of
nearly the entire history of computing?

I'm sceptical...

No, no at all but i was wondering why use my since in my scripts i only
work with global variables and dont even have one single function?

And except that i am using variables at least 2 times. One at
definition/initialization time and one in calcualation or print time.

But again, i will comply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Perfecting index.pl some more! 7
Finally a better script! 10
'\n' & <br> problem 2
Converting my index.pl(cgi) to html::template one 4
Can this be rewritten better? 17
multiples ifs 11
Games.pl again 14
CGI & Formatting 14

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top