DGT
DGT

Reputation: 2654

Using a record separator of different length

I understand that the following script would print out line(s) separated by '--' (2 dashes), but how can I use it when there are many '-' (dashes)?

{
   local $/ = "--\n";
   while (<>) {
      chomp;
      print;
   }
}

Upvotes: 1

Views: 141

Answers (1)

mob
mob

Reputation: 118645

You'll have to roll your own data stream parser. $/ isn't up to the task:

Remember: the value of $/ is a string, not a regex. awk has to be better for something. :-)


But a line that ends with three dashes and a newline is also a line that ends with two dashes and a newline. Wouldn't it be sufficient just to swap out the chomp command?

{
   local $/ = "--\n";
   while (<>) {
      chomp; s/\-+$//;    # chop off minimum record separator AND extra dashes
      print;
   }
}

or

       chomp && s/\-+$//

for the case where the last record in the data doesn't end with the record separator string.

Upvotes: 8

Related Questions