0

I have Data file that has a regular pattern from which I need to extract information. Each section is seprated by a blank line. Hence I was wondering if I could split the file for processing based in blank lines.

To better explain my problem, let me share the sample structure:

 Block: A1
 -----------------------------------
 Height:                       24.00
 Width:                         0.79
 Depth:                         0.04
 -----------------------------------

 Block: A2
 -----------------------------------
 Height:                       20.00
 Width:                         1.00
 Depth:                         0.54
 -----------------------------------

 Block: B1
 -----------------------------------
 Height:                        4.00
 Width:                         4.50
 Depth:                         0.87
 -----------------------------------

In this Database I need to simplify the report by making multiple columns. The algorithm that I am trying to achieve is that if I can split the file into smaller sections based on blank lines, I could read the database into a two-dimenstional array and finally dump the data into a format of my choice. Hence, the first requirement is to undertand in case I can split the file based on blank lines for further processing.

My expected final result is

              A1      A2     B1
 Height:     24.00  20.00   4.00
 Width:       1.00   4.00   4.50
 Depth:       0.04   0.54   0.87

Any suggestions/clues would be appreciated.

2 Answers 2

3

It's easy to split the file for processing based in blank lines using "paragraph mode".

local $/ = "";
while (my $block = <>) {
   ...
}

But it's easier not to.

my $block;
my $data;
while (<>) {
   if (/^Block:\s*(\S+)/) {
      $block = $1;
   }
   elsif (/^(\S+):\s*(\S+)/) {
      $data{$1}{$block} = $2;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0
while(<>)
{
push @B,$1 if(/Block:\s*(\S*)/);
push @H,$1 if(/Height:\s*(\S*)/);
push @W,$1 if(/Width:\s*(\S*)/);
push @D,$1 if(/Depth:\s*(\S*)/);
}

print "\t\t @B \n";
print "Height @H \n";
print "Width @W \n";
print "Depth @D \n";

Tested Here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.