Hi All,
I have written a perl code for filtering data contained in a file
($input_file_name) based on the filter expression($expr). While doing this, I face
some problems. When I hard code the arrays @data and @col ( in red color) in the code,
I am getting the results. But the moment I get the arrays @col and @data based on the
user input file ($input_file_name) inside the "if" loop, there is no filter. The
reason is @data array inside the "if" loop (red colored) when accessed outside has
only one value and that doesn't satisfy the filter expression. Then I changed it look
like the one given in blue color. Now I don't know how should I modify
1. the sub routine sub filterdata
2. my @result array
3. print join function
All the three are highlighted in blue color.
Can some one please suggest me what changes I have to do, so that I can get the
results? Any help in this regard is solicited.
#! /usr/bin/perl -w
use strict; #- matter of style
use warnings; #- and again
my (@col,@data,$input_file_name,@temp,$header,$data_line,$i);
$input_file_name= "MC_data.dat";
if (-s $input_file_name ) {
open(INPUT, $input_file_name) || die "Cannot open: \nReason: $!\n";
@temp = <INPUT> ;
chomp @temp;
$header = shift(@temp);
$header =~ s/^\s+(.*)/$1/; #Remove the leading white spaces
@col = split /\s+/, $header;
foreach $i ( 0 .. $#temp ) {
$data_line = $temp[$i];
$data_line =~ s/^\s+(.*)/$1/; #Remove the leading white spaces
@data = split /\s+/, $data_line;
$data[$i] = split /\s+/, $data_line;
}
}
@col = qw (RUN a1 a2 a3 weight sig1 sig2 sig3);
@data=(
[qw( 1 0.200000 0.200000 0.200000 0.765685 75881.9
29289.3 -46592.6)],
[qw( 2 0.200345 0.200000 0.200345 0.966661 75766.0
29268.4 -46497.6)],
[qw( 3 0.200000 0.200345 0.200000 0.766030 75867.1
29259.8 -46607.4)],
[qw( 4 0.359575 0.253987 0.359575 1.271019 43898.7
19675.6 -24223.1)],
[qw( 5 0.359921 0.253987 0.359921 1.271995 43861.3
19666.1 -24195.2)]
);
map{ my $i=$_; no strict 'refs'; *{"filter::$col[$i]"} = sub{$_->[$i]} }
0..$#col;
sub filterdata
{
my $cref=shift;
grep &$cref,@_
}
sub filterspec
{
my $e = shift;
map{ $e =~ s/\b$_\b/filter::$_()/g } @col;
eval "sub{ $e }"
}
my $expr = "weight < 1.2 && sig1 > 75800";
my $filter_handler = filterspec( $expr ) or die $@;
my @result = filterdata ($filter_handler,@data);
print join("$/",map{join(" ",@$_)[EMAIL PROTECTED]).$/;
Thanks
Regards
Guruguhan
EACoE, India.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>