CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Converting multiple lines into single line.

 



beecha
Novice

Jan 9, 2018, 4:06 AM

Post #1 of 2 (2950 views)
Converting multiple lines into single line. Can't Post

Hi All,

HAPPY NEW YEAR ALL
i am reading text file which contains below multiple lines.

i am reading every line one by one as a string.
if the line is started with WAR i need to check for '{' and make all the line into a single line till it ends with '}' without considering the '&' character.

EX:
war eligible, init =0*DEC0, ev (VARIABLE){1*BIN0,2*DEC3,6*BIN4,
& 5*BIN43,6*DEC32,
& 5*BIN43,7*DEC32}

CONVERTED TO:war eligible, init =0*DEC0, ev (VARIABLE){1*BIN0,2*DEC3,6*BIN4,5*BIN43,6*DEC32, 5*BIN43,7*DEC32}

war wear, init =1*DEC1, ev (VARIABLE) {
& 1*BIN0,2*DEC3,6*BIN4,
& 5*BIN43,6*DEC32,
& 5*BIN43,7*DEC32,6*DEC32,6*DEC3,
& 8*BIN3,6*DEC34
}

CONVERTED TO:war wear, init =1*DEC1, ev (VARIABLE) { 1*BIN0,2*DEC3,6*BIN4, 5*BIN43,6*DEC32, 5*BIN43,7*DEC32,6*DEC32,6*DEC3, 8*BIN3,6*DEC34 }

please help me with this how to convert.SmileSmile


Zhris
Enthusiast

Jan 28, 2018, 2:35 PM

Post #2 of 2 (2566 views)
Re: [beecha] Converting multiple lines into single line. [In reply to] Can't Post

Hi,


Code
use strict; 
use warnings;

while ( <DATA> )
{
next unless my $state = /^war/ .. /}\s*$/;
chomp unless $state =~ /E/;
s/^&\s*//;
print;
}

__DATA__

blah

war eligible, init =0*DEC0, ev (VARIABLE){1*BIN0,2*DEC3,6*BIN4,
& 5*BIN43,6*DEC32,
& 5*BIN43,7*DEC32}

blah

war wear, init =1*DEC1, ev (VARIABLE) {
& 1*BIN0,2*DEC3,6*BIN4,
& 5*BIN43,6*DEC32,
& 5*BIN43,7*DEC32,6*DEC32,6*DEC3,
& 8*BIN3,6*DEC34
}

blah


Output:


Code
war eligible, init =0*DEC0, ev (VARIABLE){1*BIN0,2*DEC3,6*BIN4,5*BIN43,6*DEC32,5*BIN43,7*DEC32} 
war wear, init =1*DEC1, ev (VARIABLE) {1*BIN0,2*DEC3,6*BIN4,5*BIN43,6*DEC32,5*BIN43,7*DEC32,6*DEC32,6*DEC3,8*BIN3,6*DEC34}


Chris


(This post was edited by Zhris on Jan 28, 2018, 5:43 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives