www.webdeveloper.com
Results 1 to 8 of 8

Thread: Perl, 2 per row

  1. #1
    Join Date
    Sep 2014
    Posts
    6

    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
    392
    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
    6
    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
    6
    Sorry, not used to forum script

    for
    predefined number ($num)

    read
    predefined number ($ltp)

  5. #5
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    392
    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
    6
    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
    392
    Try to increment at the end of the block

  8. #8
    Join Date
    Sep 2014
    Posts
    6
    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.

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