How can I read multiple lines of a file into blocks in Perl?

后端 未结 3 745
悲哀的现实
悲哀的现实 2021-01-29 05:57

I have a file which contains the text below.

#L_ENTRY    
#LEX        
#ROOT       
#POS        
#SUBCAT     

        
3条回答
  •  逝去的感伤
    2021-01-29 06:23

    If you set the input record separator variable to the empty string, then perl will work in paragraph mode, and return a block at a time separated by one or more blank lines in the input data

    use strict;
    use warnings 'all';
    
    local $/ = '';
    
    
    my $n;
    while (  ) {
        printf "Block %d:\n<<%s>>\n\n", ++$n, $_;
    }
    
    __DATA__
    A
    B
    C
    D
    E
    F
    
    A
    B
    C
    D
    E
    F
    

    output

    Block 1:
    <>
    
    Block 2:
    <>
    

提交回复
热议问题