How can I read multiple lines in Perl?

I just want to read a few lines in a file. For example, in sample.txt

"Hi, how are you?"
"Hello 

I'm
fine, thank you! "

Now my problem is, how can I read the second statement without deleting the line in the sentence?

EDIT:

It seems that my questions are not clear. So I think I should edit this: In my examples above, I need to get everything,

"Hello 

I'm
fine, thank you! "
while ($line = <PFILE>)
{
   #How can I get the statement to $line?
}
-1
source share
7 answers

EDIT: I think I finally understand the question:

, , - , . . . (.. "\n\n"). OP ( ).

( ). , , . , , script . , , .

#!/usr/bin/perl

use strict;
use warnings;

while (
    defined(my $q = read_statement(\*DATA))
        and defined(my $a = read_statement(\*DATA))
) {
    print "QUESTION: $q\nANSWER: $a\n\n";
}

sub read_statement {
    my ($fh) = @_;

    my $line;
    while ( $line = <$fh> ) {
        last if $line =~ /^"/;
    }
    return unless defined $line;
    return $line if $line =~ /"$/;

    my $statement = $line;
    while ($line = <$fh> ) {
        $statement .= $line;
        last if $line =~ /"$/;
    }
    return unless $statement =~ /"$/;
    return $statement;
}

:

__DATA__
"Hi how are you?"
"Hello

im
fine, thank you!"

"How is the weather?"

"It rained
all week.


It been
gray

    and cold since the 15th"

"Who are you?"

Sinan

:

C:\Temp> t
QUESTION: "Hi how are you?"

ANSWER: "Hello

im
fine, thank you!"


QUESTION: "How is the weather?"

ANSWER: "It rained
all week.


It been
gray

    and cold since the 15th"
+2

, $/:

{
    local $/;  # change the line separator to undef
    $filecontents = <FILE>;
}

, .

$lines1_and_2 = <FILE>;
$lines1_and_2 .= <FILE>;
+6

, " " , -

open MYFILE, "<", "MyFile.txt"; # The < is for read mode
while ($line = <MYfILE>) {
    foo($line); #do whatever, one line at a time
}

,

my @lines = <MYFILE>;

, , $/ undefined

{
local $/; #initialized to undef
$file = <MYFILE>;
}
+3

, , :

#!/usr/bin/env perl
use strict;
use warnings;
use Text::Balanced qw/extract_delimited/;

my $filecontents = do { local $/; <> };

while (my $item = extract_delimited($filecontents, '"')) {
    print "Item: $item\n";
}

, . ( : George , , File::Slurp.)

+3

, , " " undef-ing $/

:: Slurp - /

  use File::Slurp;

  my $text = read_file( 'filename' ) ;
  my @lines = read_file( 'filename' ) ;

  write_file( 'filename', @lines ) ;

  use File::Slurp qw( slurp ) ;

  my $text = slurp( 'filename' ) ;
+1

, " " , , . :

my $content = join "", <>;
my @statements = ();
push @statements, $1 while $content =~ /"(.*?)"/msg;

This does not handle hidden double quotes in your quoted values, but your example has no examples of this. If you need to avoid quotes, you need to modify the regex slightly or use Text :: Balanced as described above.

+1
source

With the OP explaining that he is trying to get the quoted lines from the file and assuming that each line closing the quote will be at the end of the line, my approach would be:

#!/usr/bin/perl

use strict;
use warnings;

local $/ = qq("\n);    # Extra " to fix SO syntax highlighting

while (my $quot_text = <DATA>) {
  print "Next text:\n$quot_text\n"
}

__DATA__
"Hi how are you?"
"Hello 

im
fine, thank you!"

What returns:

Next text:
"Hi how are you?"

Next text:
"Hello

im
fine, thank you!"
+1
source

Source: https://habr.com/ru/post/1718667/


All Articles