Looping almost the same repetitive lines

Discussion in 'Perl Misc' started by Nikos, Apr 22, 2005.

  1. Nikos

    Nikos Guest

    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.
     
    Nikos, Apr 22, 2005
    #1
    1. Advertising

  2. Nikos wrote:
    > 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} )),

    <snip other rows>

    >
    > 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
     
    Mark Clements, Apr 22, 2005
    #2
    1. Advertising

  3. Nikos

    Nikos Guest

    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.
     
    Nikos, Apr 22, 2005
    #3
  4. Nikos wrote:
    > 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

    <snip>

    > 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
     
    Mark Clements, Apr 22, 2005
    #4
  5. Nikos

    Nikos Guest

    Mark Clements wrote:

    > 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
    >
    > <snip>
    >
    >> 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?


    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.
     
    Nikos, Apr 22, 2005
    #5
  6. Nikos <> wrote:

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



    OK, I've had enough.

    *plonk*


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 22, 2005
    #6
  7. Nikos

    Nikos Guest

    Tad McClellan wrote:
    > Nikos <> wrote:
    >
    >
    >>ps. its ok about the my i actually never use them.


    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, Apr 22, 2005
    #7
  8. Nikos wrote:

    >
    > $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
     
    Mark Clements, Apr 22, 2005
    #8
  9. Nikos wrote:
    > Tad McClellan wrote:
    >
    >> Nikos <> wrote:
    >>
    >>
    >>> ps. its ok about the my i actually never use them.

    >
    >
    > 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
     
    Mark Clements, Apr 22, 2005
    #9
  10. Nikos <> wrote:
    > Tad McClellan wrote:
    >> Nikos <> wrote:
    >>
    >>
    >>>ps. its ok about the my i actually never use them.

    >
    > 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...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 23, 2005
    #10
  11. Mark Clements <> wrote:


    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 23, 2005
    #11
  12. Nikos

    Nikos Guest

    Tad McClellan wrote:

    > 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.
     
    Nikos, Apr 23, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry

    Re: Repetitive code (Newbie)

    Jerry, Aug 6, 2003, in forum: VHDL
    Replies:
    2
    Views:
    1,049
    Mike Treseler
    Aug 7, 2003
  2. smu
    Replies:
    0
    Views:
    938
  3. Urs Thuermann
    Replies:
    2
    Views:
    328
    Jorgen Grahn
    Nov 3, 2011
  4. Replies:
    5
    Views:
    279
  5. Piet van Oostrum
    Replies:
    0
    Views:
    121
    Piet van Oostrum
    Mar 7, 2013
Loading...

Share This Page