guhan has asked for the wisdom of the Perl Monks concerning the following question:
#! /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 s +paces @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(" ",@$_)}@result).$/;
Edit: Added <code> tags, minor formatting. larsen
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Array accessing
by matija (Priest) on Mar 04, 2004 at 14:21 UTC |