Q:

How to read tabular data from text file - Perl

We have text file which is having data in normal as well as tabular form. i can read normal data but i am unable to read the data which is in tabular form.
Can anyone please help me out to read the and extract the tabular data. 
Text File Data : 
    225 Top Hitters
    RT(ms)    BRT(ms)    TL(ms)    l_mig_a    l_mig_w    b_mig_a    b_mig_w    l_b_mig_a    l_b_mig_w    b_l_mig_a    b_l_mig_w
    --------  ---------  --------  ---------  ---------  ---------  ---------  -----------  -----------  -----------  -----------
     11078.9      141.3    3754.8        418       7325          0          0            0            4            0            4

Total active inter-cluster migrations: 0
Total wakeup inter-cluster migrations: 8
Total active migrations: 418
Total wakeup migrations: 7333

My Code: 
    use strict;
    use warnings;
    my ($RT,$BRT,$TL ,$l_mig_a,$l_mig_w,$b_mig_a,$b_mig_w,$l_b_mig_a,$l_b_mig_w,$b_l_mig_a,$b_l_mig_w);
    open (FH, "<" ,"file.txt") or print "could not open $!";
    my @lines = <FH>;
    close FH;
    foreach my $line (@lines) {
        print "$line \n";
    }

Expected Output : 
$RT = 11078.9
$BRT = 141.3
$TL = 3754.8
$l_mig_a = 418
$l_mig_w = 7325
$b_mig_a = 0
$b_mig_w = 0
$l_b_mig_a = 0
$l_b_mig_w = 4
$b_l_mig_a = 0
$b_l_mig_w = 4

A:

You can "slurp" whole file into single string variable and use regular expression to parse tabular data. Below please find sample script with subroutine to simplify generating regular expression.
Below please find sample implementation with test data bundled with code into single script/file.
use strict;
use warnings;

my $text;
{
  # put all lines into single string
  local $/ = undef;
  $text = <DATA>;
}

my $regex = &make_regex(qw{RT(ms)    BRT(ms)    TL(ms)    l_mig_a    l_mig_w    b_mig_a    b_mig_w    l_b_mig_a    l_b_mig_w    b_l_mig_a    b_l_mig_w});

print "REGEX-START\n$regex\nREGEX-END\n"; # Debuging: Show generated regular expression

my ($RT,$BRT,$TL ,$l_mig_a,$l_mig_w,$b_mig_a,$b_mig_w,$l_b_mig_a,$l_b_mig_w,$b_l_mig_a,$b_l_mig_w) 
   = $text =~ /$regex/ or die;

print "b_l_mig_w = $b_l_mig_w\n";

sub make_regex {
  my $n = scalar(@_);
  my $str = '
\s*' . join('\s+',map {quotemeta($_)} @_) . '\s*
\s*' . join('\s+',('-+')    x $n) . '\s*
\s*' . join('\s+',('(\S+)') x $n) . '\s*
';
  qr{$str}m;
} # end sub make_regex

__DATA__
    225 Top Hitters
    RT(ms)    BRT(ms)    TL(ms)    l_mig_a    l_mig_w    b_mig_a    b_mig_w    l_b_mig_a    l_b_mig_w    b_l_mig_a    b_l_mig_w
    --------  ---------  --------  ---------  ---------  ---------  ---------  -----------  -----------  -----------  -----------
     11078.9      141.3    3754.8        418       7325          0          0            0            4            0            4

Total active inter-cluster migrations: 0
Total wakeup inter-cluster migrations: 8
Total active migrations: 418
Total wakeup migrations: 733

