agustina_s has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks... I have a very big file consists of more than 100 records and each record is separated by //\n. Each record can consist of a few pages. What do I have to do if I want to first do while loop for each record and then for each line in the record?? this is the part of the code where each record have been separated :
$/ = "\/\/\n"; $counter=1; while (<>){ # Read the each record and return one whole record $entry = SWISS::Entry->fromText($_); print "Entry $counter\n"; #read each line of the record?? }
Thanks so muchhh...

Replies are listed 'Best First'.
Re: how to read the line of each record??
by seattlejohn (Deacon) on Jan 22, 2002 at 09:06 UTC
    Something like this might do the job for you:
    while (<>){ $entry = SWISS::Entry->fromText($_); print "Entry $counter\n"; foreach my $one_line (split /\n/,$entry) { # do something with each line now } }

    This assumes that the Entry->fromText() method is returning the text from the file in one big string that you simply want to break on newlines.

    If you're really going to be processing a bunch of text like this, you may also want to run with use strict and use warnings, test your file opens/reads to make sure they worked properly, and otherwise armor-plate your code.

Re: how to read the line of each record??
by VSarkiss (Monsignor) on Jan 22, 2002 at 09:07 UTC

    By "each line in the record", I presume you mean that a record -- that is, chunks separated by //\n -- should be further split where newlines \n occur (hint, hint). After you've read a chunk into $_, you should be able to get the "lines" with split:

    while (<>){ # Read the each record and return one whole record $entry = SWISS::Entry->fromText($_); my @lines = split /\n/; # Now each element of @line is one "line" from the # block just read in. The last element is //. }
    By the way, you don't need to escape the forward slashes when you assign a value to $/; that's a string, not a regexp, as explained in perlvar (the part that says awk has to better for something ;-). (In other words, you can just do $/ = "//\n";

    HTH