www.webdeveloper.com
Results 1 to 3 of 3

Thread: Printing records in Perl

  1. #1
    Join Date
    Jun 2008
    Posts
    223

    Printing records in Perl

    Let's say I have a file and there are 8 records in it.
    I would like to print the whole file 3 records at a time.
    The file will never be large (maybe a dozen or so records at most).

    I tried to find an answer for this, however, found everything but.

    Code:
    use strict;
    use warnings;
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    print "Content-type: text/html\n\n";
    
    my $nol="3"; # number of records per row
    
    while(<DATA>) {
    my $count = $.;
    
     my @pl =split /\|/, $_;
    
      print qq ~($pl[0])$pl[1] ~; # Each row is three records
    
     }
    
    __DATA__
    aaa|one
    bbb|two
    ccc|three
    ddd|four
    eee|five
    fff|six
    ggg|seven
    hhh|eight
    How do I make this work?

    Thanks in advance

  2. #2
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    391
    Code:
    sub get_record {
        my $record = <DATA>;
        chomp $record;
        return $record
    }
    
    my @records_buf;
    while (my $record = get_record()) {
        push @records_buf, $record;
        if (@records >= 3) {
            flush();
        }
    }
    flush();
    
    sub flush {
        print join(';', @records_buf), "\n";
        @records_buf = ();
    }

  3. #3
    Join Date
    Jun 2008
    Posts
    223
    Thanks Sixtease, not seen anything quite like this before. Interesting.

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