On 2/28/06, Cinzia Sala <[EMAIL PROTECTED]> wrote:
snip
>      }    until ($/="//end");
snip

You have two problems here.  The first is that you are doing an
assignment (=) not a comparison (==), so the result is always true
(hence the printing of only one record).  The second is a
misunderstand of the $/ variable.  $/ is the record separator.  By
default it is "\n", but you are setting it to "//\n".  Its use in the
logical expression $/ == "//end" makes no sense since it never changes
(unless changed by the programmer).  What you want is something like
this:

{ #these braces and the local keyword limits the scope of the change to $/
    local $/ = "//\n";
    while (my $record = <DBFILE>) {
        chomp($record); #remove the record separator //\n
        print "$record\n";
    }
}

This code still has a problem, namely the fact that the end of file
indicator ("//end\n") is not the same as the end of record separator
("//\n").  So, you need to handle that as well:


{ #these braces and the local keyword limits the scope of the change to $/
    local $/ = "//\n";
    while (my $record = <DBFILE>) {
        last if $record =~ m{^//END\n$}; #stop processing at the end
of file indicator
        chomp($record); #remove the record separator //\n
        print "$record\n";
    }
}

You also asked "How can I print in separate files the other records?".
 The answer depends on what those filenames should be.  If you wish
one record per file with the name of the file being record1, record2,
record3, etc. then the following code will work.

{ #these braces and the local keyword limits the scope of the change to $/
    local $/ = "//\n";
    my $i = 1;
    while (my $record = <DBFILE>) {
        last if $record =~ m{^//END\n$}; #stop processing at the end
of file indicator
        chomp $record ; #remove the record separator //\n
        open my OUT, '>', "record$i";
        print OUT "$record\n";
        close OUT;
        $i++;
    }
}

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to