Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get rid of an infinite loop

Status
Not open for further replies.

szzxy

Technical User
Aug 14, 2010
8
CA
Dear programmer(s),
Could you please tell me why the below script runs infinitely ? How can I print the result only once?



#!/usr/local/bin/perl -w
use strict;

open (file1, $ARGV[0]);

my $gene;
my $rs;
my $tf;
my $ds;
my %gene_key;
my @genes;



while(<file1>){

if ($_=~/^(.+)\t+(.+)\t+(.+)\t(.+)$/){
$gene=$1;
$rs=$2;
$tf=$3;
$ds=$4;
}



push (@genes, $gene);

foreach $gene (@genes){

$gene_key{$gene}=($3." ".$2." ".$4);

}


foreach $gene(keys %gene_key){

my @gene_table;

push (@gene_table, $gene," ",$gene_key{$gene});

print "$gene.' '.$gene_key{$gene}";

}


}

close (file1);


Regards,
szzxy
 
Really?

Code:
while(<file1>){

Why would you not expect this to run infinitely?

In your case, you have to do something with file1 to make it stop (such as delete it, move it, whatever.)

What you have is the same as:
Code:
while(1)

Unless I'm missing something, than in that case I apologize in advance for the sarcasm.
 
Hi,

You're printing the current contents of the entire hash for every line in the file. The 2nd FOREACH statement should be outside/after the WHILE statement.

If you are still having problems, I suggest you provide example input with expected output, as the code you provided can most likely be simplified.

Chris
 
Really?

kodr said:
Why would you not expect this to run infinitely?

Why would you expect it to run infinitely? It stops when it reaches the end of the input file.

szzxy, I think you have just placed your second foreach loop in the wrong place, so you are ending up with a cartesian product of the input data (not exactly infinite, but could be very large for a significant amount of input).

If you place it outside the while (<file1>) { ... } loop I think you will get the results you desire.

Annihilannic.
 
I forgot to mention, I don't think your print statement looks quite right, perhaps this will give you better results:

Code:
    print "$gene $gene_key{$gene}\n";

Annihilannic.
 
Perhaps this is what you are trying to achieve:

Code:
#!/usr/bin/perl
use strict;
use warnings;

my @gene_table;
my %gene_key;

open my $input, '<', $ARGV[0] or die "cannot open input - $!";

while(my $line = <$input>){
	chomp $line;
	my ($gene, $rs, $tf, $ds) = split /\t+/, $line;
	$gene_key{$gene} = "$tf $rs $ds";
}

while (my ($gene, $values) = each %gene_key) {
	my $string = "$gene $values\n";
	push (@gene_table, $string);
	print "$string\n";
}

close $input;
Code:
2A 2C 2B 2D
1A 1C 1B 1D
3A 3C 3B 3D

Goodluck,

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top