http://qs321.pair.com?node_id=912025


in reply to Infinite regex loop...

Because $SIRtrue is true whenever /SPECint_base/ matches, this line:

if ($SIRtrue && !($_ =~ /SPECint_base/)){

is equivalent to:

if ($SIRtrue && !$SIRtrue){

That "if" statement can never be true. A similar argument applies to the final "if" statement. No assignment will ever be made to the %SIR or %SFR hashes.

You might also wish to note that regex matches in Perl are performed by default on the $_ variable. See perlretut. So you can replace:

if ($_ =~ /SPECint_base/){

with this:

if (/SPECint_base/){

Update: of course limzz's reply is correct. But we still have not heard from the OP exactly what the behaviour of the program is. There is nothing in the code shown to cause an infinite loop.

Replies are listed 'Best First'.
Re^2: Infinite regex loop...
by tj_thompson (Monk) on Jun 29, 2011 at 22:01 UTC

    Your code, for the most part, seems to do what you want. I don't see how you can end up in a loop. The only portion that seems very suspect is this:

    if ($SIRtrue && !($_ =~ /SPECint_base/)){ $SIR{$_}=$_; } if ($SFRtrue && !($_ =~ /SPECfp_base/)){ $SFR{$_}=$_; }

    You're stuffing the line you find into a hash key named the same as the line value. This is likely not what you want.

    This may be closer to what you want (assuming my data below somewhat resembles your file), but it's honestly hard to say:

    use strict; use warnings; use Data::Dumper; my $str = <<END; SPECint_base 1 2 3 SPECfp_base 4 5 6 END open my $fh, '<', \$str or die "Could not open for read:$!\n"; my $SIRtrue; my $SFRtrue; my %SIR = (); my %SFR = (); while (<$fh>){ chomp; if (/SPECint_base/){ $SIRtrue = 1; $SFRtrue = 0; next; } elsif (/SPECfp_base/){ $SIRtrue = 0; $SFRtrue = 1; next; } if ($SIRtrue)){ push( @{$SIR{SPECint_base}}, $_ ); } elsif ($SFRtrue){ push( @{$SFR{SPECfp_base}}, $_ ); } } print Dumper(\%SIR); print Dumper(\%SFR);

    Output:

    $VAR1 = { 'SPECint_base' => [ '1', '2', '3' ] }; $VAR1 = { 'SPECfp_base' => [ '4', '5', '6' ] };
Re^2: Infinite regex loop...
by limzz (Novice) on Jun 29, 2011 at 20:23 UTC

    Nope, maybe I should have explained what the code does. Think of SPECint_base and SPECfp_base as headers, then any elements beneath them will be put into the hash, but not the headers themselves.