0

Hello I need help with perl script I'm writing. we have an input file that contains following:

variable=value;
variable2=value2;
.
.
.

However sometimes following thing happens - one of the variable might have its value written in more lines:

variable=value;
variable=value value

value value value

value

;
variable2=value2;
.
.
.

Question: Can you please recommend a good way to read the value spread over more lines? Is there a way to say: start reading from here and finish when you reach ';' character? thank you!

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by d5e5
0

The input record separator (see perlvar) determines how much input to read for each input record. It is newline by default. Change it to semicolon followed by newline and each time perl reads the file it will read up to the next semicolon followed by newline, which is what you want.

#!/usr/bin/perl
use strict;
use warnings;

$/ = ";\n";#Now semicolon followed by newline character indicates end of input rec

while (<DATA>){
    chomp;
    my ($name, $value) = split(/=/);
    print "$name => $value\n";
}

__DATA__
variable=value;
variableX=value value

value value value

value

;
variable2=value2;

Edited by d5e5: Add link to perlvar

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.