Subject: | bug in match_pattern sub |
Hello,
I have installed your Table module and found out that match_pattern
subroutine doesn't work as I expected. I attach a test file and test
script for you to see.
I have hacked Table.pm to make it work -- it seems a bug was in the '...
map ...' line of sub match_pattern.
I am quite new to Perl and map function is still quite confusing to me,
so I copied and modified the calls from match_string:
orig sub:
#######################
sub match_pattern {
my ($self, $pattern) = @_;
my @data=();
$self->rotate() if $self->{type};
@Table::OK= eval "map {$pattern} \@{\$self->{data}};"; #<--- this is
where the bug is hiding
for (my $i=0; $i<$self->nofRow(); $i++) {
push @data, $self->{data}->[$i] if $Table::OK[$i];
my $tmp = $self->{data}->[$i];
print "pushing array @$tmp \n" if $Table::OK[$i];
}
return new Table(\@data, \@{$self->{header}}, 0);
}
#######################
working version:
sub match_pattern {
my ($self, $pattern) = @_;
my @data=();
$self->rotate() if $self->{type};
#miro hack start:
@Table::OK=();
foreach (@{$self->data}) {
# print "PATTERN $pattern \n";
# print "2nd column " . $_->[2] ." matches $pattern\n" if (eval
$pattern); #matches what is supposed to
# print "2nd columnB " . $_->[2] ." matches $pattern\n" if
($pattern); #will match other rows too
push @Table::OK, undef;
if (eval $pattern) { #this 'eval' fixed things
push @data, $_; #row_ref;
$Table::OK[$#Table::OK]=1;
}
}
#done miro
return new Table(\@data, \@{$self->{header}}, 0);
}
#######################
Table: $VERSION = '1.16';
Perl: This is perl, v5.8.8 built for x86_64-linux-thread-multi
Distro: CentOS Linux 2.6.18-164.el5 x86_64 GNU/Linux
Otherwise I really appreciate this package. Its quite intuitive to use
and very helpful Thank you very much!
Cheers,
miro
Subject: | test_tbl.pl |
#!/usr/bin/perl -w
use strict;
use Table;
my $t = Table::fromCSV("$ARGV[0]"); # Read a csv file into a table oject
#print "got table.\n";
my $cur_row = $t->match_pattern('$_->[2]=~/M/');
print $cur_row->html;
Subject: | test.csv |
measure,"Group","Sex","Mean","Std. Deviation","N"
"Fregly - Stand on Right Leg; Eyes Open","NAC","Female","116.75","12.811","32"
,,"Male","120.00",".000","20"
,,"Total","118.00","10.115","52"
,"LTAA","Female","115.32","13.871","34"
,,"Male","117.38","16.641","48"
,,"Total","116.52","15.495","82"
,"Total","Female","116.02","13.284","66"
,,"Male","118.15","13.990","68"
,,"Total","117.10","13.638","134"
"Fregly - Stand on Right Leg; Eyes Closed","NAC","Female","89.06","40.531","32"
,,"Male","76.70","43.957","20"
,,"Total","84.31","41.896","52"
,"LTAA","Female","69.21","47.543","34"
,,"Male","67.44","46.994","48"
,,"Total","68.17","46.937","82"
,"Total","Female","78.83","45.067","66"
,,"Male","70.16","45.992","68"
,,"Total","74.43","45.575","134"
"Fregly - Stand on Left Leg; Eyes Open","NAC","Female","53.56","15.354","32"
,,"Male","58.10","5.990","20"
,,"Total","55.31","12.713","52"
,"LTAA","Female","53.47","15.106","34"
,,"Male","49.71","17.677","48"
,,"Total","51.27","16.666","82"
,"Total","Female","53.52","15.109","66"
,,"Male","52.18","15.627","68"
,,"Total","52.84","15.331","134"
"Fregly - Stand on Left Leg; Eyees Closed","NAC","Female","20.91","16.241","32"
,,"Male","24.05","20.374","20"
,,"Total","22.12","17.815","52"
,"LTAA","Female","17.38","14.771","34"
,,"Male","15.65","16.425","48"
,,"Total","16.37","15.690","82"
,"Total","Female","19.09","15.483","66"
,,"Male","18.12","17.940","68"
,,"Total","18.60","16.719","134"
"Fregly - Sharpened Romberg; Eyes Open","NAC","Female","55.97","12.765","32"
,,"Male","56.55","11.255","20"
,,"Total","56.19","12.096","52"
,"LTAA","Female","51.74","15.524","34"
,,"Male","48.08","19.336","48"
,,"Total","49.60","17.844","82"
,"Total","Female","53.79","14.304","66"
,,"Male","50.57","17.700","68"
,,"Total","52.16","16.137","134"
"Fregly - Sharpened Romberg Eyes Closed","NAC","Female","21.13","16.556","32"
,,"Male","25.85","19.768","20"
,,"Total","22.94","17.821","52"
,"LTAA","Female","15.79","13.052","34"
,,"Male","12.56","11.698","48"
,,"Total","13.90","12.303","82"
,"Total","Female","18.38","14.981","66"
,,"Male","16.47","15.621","68"
,,"Total","17.41","15.281","134"
"Fregly - Walk-on-floor Eyes Open","NAC","Female","19.13","3.035","32"
,,"Male","20.00",".000","20"
,,"Total","19.46","2.405","52"
,"LTAA","Female","18.24","4.411","34"
,,"Male","18.92","3.363","48"
,,"Total","18.63","3.822","82"
,"Total","Female","18.67","3.804","66"
,,"Male","19.24","2.861","68"
,,"Total","18.96","3.358","134"
"Fregly - Walk-on-floor Eyes Closed","NAC","Female","15.41","6.153","32"
,,"Male","12.80","6.178","20"
,,"Total","14.40","6.235","52"
,"LTAA","Female","12.53","6.219","34"
,,"Male","12.90","6.521","48"
,,"Total","12.74","6.361","82"
,"Total","Female","13.92","6.308","66"
,,"Male","12.87","6.376","68"
,,"Total","13.39","6.341","134"