perl - Regex value comparison -
i want compare 2 numbers isolated sample data:
'gi|112807938|emb|cu075707.1|_xenopus_tropicalis_finished_cdna,_clone_tneu129d01 c1:tcons_00039972(xloc_025068),_12.9045:32.0354,_change:1.3118,_p:0.00025,_q:0.50752 c2:tcons_00045925(xloc_029835),_10.3694:43.8379,_change:2.07985,_p:0.0004,_q:0.333824', 'gi|115528274|gb|bc124894.1|_xenopus_laevis_islet-1,_mrna_(cdna_clone_mgc:154537_image:8320777),_complete_cds c1:tcons_00080221(xloc_049570),_17.9027:40.8136,_change:1.18887,_p:0.00535,_q:0.998852 c2:tcons_00092192(xloc_059015),_17.8995:35.5534,_change:0.990066,_p:0.0355,_q:0.998513', 'gi|118404233|ref|nm_001078963.1|_xenopus_(silurana)_tropicalis_pancreatic_lipase-related_protein_2_(pnliprp2),_mrna c1:tcons_00031955(xloc_019851),_0.944706:5.88717,_change:2.63964,_p:0.01915,_q:0.998852 c2:tcons_00036655(xloc_023660),_2.31819:11.556,_change:2.31757,_p:0.0358,_q:0.998513',
using following regex:
#!/usr/bin/perl -w use strict; use file::slurp; use data::dumper; $data::dumper::sortkeys = 1; (@log_change, @largest_change); foreach (@intersect) { chomp; @condition1_match = ($_ =~ /c1:.*?change:(-?\d+\.\d+)|c1:.*?change:(-?inf)/); # value 'inf' or '-inf'. allows either numerical or inf value captured. @condition2_match = ($_ =~ /c2:.*?change:(-?\d+\.\d+)|c2:.*?change:(-?inf)/); push @log_change, "@condition1_match\t@condition2_match"; } print dumper (\@log_change);
which gives output:
'1.3118 2.07985 ', '1.18887 0.990066 ', '2.63964 2.31757 ',
ideally, within same loop want make comparison between values held in @condition1_match
, @condition2_match
such larger value pushed onto new array, unless comparing against non numerical 'inf' in case push numerical value.
something this:
my (@log_change, @largest_change); foreach (@intersect) { chomp; @condition1_match = ($_ =~ /c1:.*?change:(-?\d+\.\d+)|c1:.*?change:(-?inf)/); @condition2_match = ($_ =~ /c2:.*?change:(-?\d+\.\d+)|c2:.*?change:(-?inf)/); push @log_change, "@condition1_match\t@condition2_match"; unless ($_ =~ /change:-?inf/) { if (@condition1_match > @condition2_match) { push @largest_change, @condition1_match; } else { push @largest_change, @condition2_match; } } } print dumper (\@largest_change);
which gives:
'2.07985', undef, '0.990066', undef, '2.31757', undef,
as lot of error message:
use of uninitialized value $condition2_match[1] in join or string @ intersect.11.8.pl line 114.
i'm unsure error message means, why i'm getting undef values in @largest_change
as you've written code, @condition_match1
, @condition_match2
created 2 elements -- corresponding 2 capture groups in regular expression -- each time there match. 1 of these elements undef
, leading uninitialized ...
warnings.
in case, can repair program putting |
inside capture group:
my ($condition1_match) = ($_ =~ /c1:.*?change:(-?\d+\.\d+|-?inf)/); ($condition2_match) = ($_ =~ /c2:.*?change:(-?\d+\.\d+|-?inf)/);
so there single capture group , matching operation produces list single, defined element.
in addition, comparison
if (@condition1_match > @condition2_match) {
is not doing think doing. in perl, numerical comparison between 2 arrays comparison of array lengths. apparently mean compare defined value in each of arrays, need more cumbersome like:
my $condition1_match = $condition1_match[0] // $condition1_match[1]; $condition2_match = $condition2_match[0] // $condition2_match[1]; if ($condition1_match > $condition2_match) { push @largest_change, $condition1_match; } else { push @largest_change, $condition2_match; }
Comments
Post a Comment