in reply to Re^8: Join multiple lines in a string based on regex.
in thread Join multiple lines in a string based on regex.

Fixed by deleting leading white space, in my code:
use strict; use warnings; my $row =""; my $inarray=0; while (<DATA>) { chomp; s/^\s+//; # <-- New line added <---- if (/^array|^unassigned/){ print "$row\n" if $inarray; $row=$_; $inarray=m/^array/; next; } next unless $inarray; $row .= " $_"; } print "$row\n" if $inarray; __DATA__

        "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Replies are listed 'Best First'.
Re^10: Join multiple lines in a string based on regex.
by pr33 (Scribe) on Dec 22, 2014 at 20:16 UTC
    Thanks that works , But I can't use the value of $row variable for future purpose even within the while loop . I tried few things , But it didn't work .
      "didn't work" is a very poor problem description.

      If you would like help with this, please explain what you tried, what you were expecting, and what you got.

              "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

        Here is what I am trying to do . Basically , I am using a list slice to extract few elements from the O/P and I wanted to assign the result to a variable within the same Open While File Handle so that I can use the result for other purpose . I am splitting the data with : and \s as delimiter and extracting few elements of the resulting array . When I use the assignment , It cannot print the value of the variables .
        if (/^array|^unassigned/){ print "$row\n" if $inarray; $row=$_; my (undef, $unit, undef, undef , undef, undef , undef, $port, +undef, undef, undef, undef, undef,undef) = split /[\s,:]+/, $row; $inarray=m/^array/; next; } next unless $inarray; $row .= " $_"; } close $fd;