www.webdeveloper.com
Results 1 to 11 of 11

Thread: Perl, 2 per row

  1. #1
    Join Date
    Sep 2014
    Posts
    11

    Perl, 2 per row

    Hi, Ive been try to get 2 lines to print into table rows and had no success at all. So need some help on it please.

    Code:
    #!/usr/bin/perl
    
    ## ---- Print 2 lines per table row (without a module).
    use strict;
    use warnings;
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    print "Content-type: text/html\n\n";
    my $num = 4;
    my $start = 0;
    my $ltp = 2; # Number of lines for each row.
                   
    print qq ~<table border="1">~;
     while (my $line = <DATA>) {
      chomp; $start = $start+1;
      if ($start <= $num) {
      ##I need 2 lines in each row - not a clue how to do that.
      print qq ~<TR><td>$line</td><td>$line</td></TR>\n~;
      }
    }
    print qq ~</table>~;
    exit(0);
    
    __DATA__
    Aaaa
    Bbbb
    Cccc
    Dddd
    Eeee
    Ffff
    Gggg
    Hhhh
    Iiii
    Thanks in advance.

  2. #2
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    393
    Instead of </td><td>, try to say <br />. Please note that this is not a perl problem. It is an HTML one.

  3. #3
    Join Date
    Sep 2014
    Posts
    11
    Hi Sixtease, it is a Perl problem. Let's forget the HTML (didn't mean to confuse you).

    "line" represents an ID out of my database (not SQL or the like). End result will have to be each ID in a table cell because there will be more stuff added.

    I need a result like this:
    Code:
    one	two
    three	four
    five	six
    I guess it is a counting problem (I'm terrible at numbers), where I reach the predefined number ($num) for each line, then Perl breaks it for the next line.

    Hope that makes sense.

  4. #4
    Join Date
    Sep 2014
    Posts
    11
    Sorry, not used to forum script

    for
    predefined number ($num)

    read
    predefined number ($ltp)

  5. #5
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    393
    How about you print new row when you had enough records processed? Like
    Code:
    print '</tr><tr>' if $start % $ltp == 0
    Sorry cannot write a decent example as I am on a phone.

  6. #6
    Join Date
    Sep 2014
    Posts
    11
    Sorry didn't back sooner, had a back injury and wasn't up to much.

    Forget the table for now, I can do that once working.

    Managed to get this far, but the first line isn't being seen.
    Code:
    my $num = 4; # total number of $lines
    my $start = 0;
    my $ltp = 2; # Number of $lines for each row.
    
     while (my $line = <DATA>) {
      chomp;
      $start = $start+1;
      if ($start <= $num) {
      my @fl="$line";
        foreach $line (@fl) {
        chomp($line);
        @fl=split /\n/, "$line";
    ### misses Aaaa (the first one)
         if ($start % $ltp == 0) {
          $ltp = $ltp+1;
          print qq ~$fl[0] \n~;
         } # end %
        } # end foreach
      } # end <=
     } # end while
    May not be best way but at least got this far

  7. #7
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    393
    Try to increment at the end of the block

  8. #8
    Join Date
    Sep 2014
    Posts
    11
    Wasn't quite sure what to do. I've had to stop because my spine hurts too much, so can't spend much time on computer. Will have to scrap the project. Thanks a lot for your help anyways Sixtease.

  9. #9
    Join Date
    Sep 2014
    Posts
    11
    Hi Sixtease, feeling better now and decided to have another try at it. I managed to get the result in rows.

    Code:
    use strict;
    use warnings;
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    print "Content-type: text/html\n\n";  
    my $pt="1";
    my $num="3";
    my $rows="2"; ## number of table rows to show
    
    print qq ~<table border="1"><tr>~;
    while (my $line = <DATA>) {
    chomp($line);
     my @fd=split /\|/, "$line";
      if ($pt == $num) {print "<td>$fd[0]<br>$fd[1]</td></TR><TR>\n"; $pt=1;} # end row
      else {if ($fd[0] ne "") {print "<td>$fd[0]<br>$fd[1]</td>\n"; $pt++;}} # begin row
    } # end while
    print qq ~</tr></table>~;
    exit(0);
    
    __DATA__
    01|Aaaa|
    02|Bbbb|
    03|Cccc|
    04|Dddd|
    05|Eeee|
    06|Ffff|
    07|Gggg|
    08|Hhhh|
    09|Iiii|
    10|Jjjj|
    11|Kkkk|
    12|Llll|
    The thing now is, how do I make it so that I can only show a predefined number of rows ($rows)?

    Thanks in advance

  10. #10
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    393
    Code:
    use strict;
    use warnings;
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    print "Content-type: text/html\n\n";  
    my $pt="1";
    my $num="3";
    my $rows="2"; ## number of table rows to show
    
    print qq ~<table border="1"><tr>~;
    my $i = 0;
    while (my $line = <DATA>) {
     last if $i >= $rows; # exit while-loop after $rows lines
     chomp($line);
     my @fd=split /\|/, "$line";
      if ($pt == $num) {print "<td>$fd[0]<br>$fd[1]</td></TR><TR>\n"; $pt=1; $i++} # end row
      else {if ($fd[0] ne "") {print "<td>$fd[0]<br>$fd[1]</td>\n"; $pt++;}} # begin row
    }# end while
    print qq ~</tr></table>~;
    exit(0);
    
    __DATA__
    01|Aaaa|
    02|Bbbb|
    03|Cccc|
    04|Dddd|
    05|Eeee|
    06|Ffff|
    07|Gggg|
    08|Hhhh|
    09|Iiii|
    10|Jjjj|
    11|Kkkk|
    12|Llll|

  11. #11
    Join Date
    Sep 2014
    Posts
    11
    Code:
    my $i = 0;
    .....
     last if $i >= $rows; # exit while-loop after $rows lines
    .....
    $i++
    Oh! It's that simple!

    I thought it would be some complicated mathematical equation. I did try all kinds of stuff and got absolutely nowhere because I had that train of thought.

    Hey Sixtease, thanks a lot man, that's really great.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles