Hi...
I am trying to make a program that ask for a specific DNA number, where the program have to check if the given number is in a specific given list... here the program should tell me if they are or arent in the list..the program runs untill I write stop.... hope someone can help me...

had tried this:

use strict;


# step 1. read the file:


my $accessionno = ' ';
my $line = ' ' ;
my @arr = ( );
my $match = ' '; #was anything found?
my $search = ' '; # thing to seach for
my $element_position = ' ';
my $found = ' ' ;
open( IN, '<', "clean1.acc") or die "cant read file\n";


while (defined ($line = <IN>)) {
chomp $line;
push @arr, $line;
}
close IN;


# step 2.ask for accession number untile it stop using STOP , also searshing the number:


while($accessionno ne "STOP"){
print " which Accession number are you searching for?\n";


chomp ($accessionno= <STDIN>);


for($element_position= $#arr; $element_position >= 0 ; $element_position--) {
if($arr[$element_position] eq $accessionno) {
print "congratulation the accession number is in the given list!\n";
}else{
if (!$match){
print "nothing found!\n"
}
}
}
}
__END__

I dont know what I am doing wrong but i can figure out that i have to save the found number ......?

Thanx

Recommended Answers

All 6 Replies

what does the data look like?
what does the number you are looking for look like?
why are you going through the array backwards?
what is $match being used for?

Hi...
I am trying to make a program that ask for a specific DNA number, where the program have to check if the given number is in a specific given list... here the program should tell me if they are or arent in the list..the program runs untill I write stop.... hope someone can help me...

I just go through your code, and I guess you had problem with $match. OR you were not able to print msg. saying the number is NOT found. Try this.. I just modified you code..

use strict;

# step 1. read the file:

my $accessionno = ' ';
my $line = ' ' ;
my @arr = ( );
my $match = ' '; #was anything found?
my $search = ' '; # thing to seach for
my $element_position = ' ';
my $found = ' ' ;
open( IN, '<', "clean1.acc") or die "cant read file\n";

while (defined ($line = <IN>))
{
	chomp $line;
	push @arr, $line;
}
close IN;

# step 2.ask for accession number untile it stop using STOP , also searshing the number:

while($accessionno ne "STOP")
{
	print " which Accession number are you searching for?\n";

	chomp ($accessionno= <STDIN>);

	for($element_position= $#arr; $element_position >= 0 ; $element_position--) 
	{
		if($arr[$element_position] eq $accessionno) 
		{
			print "congratulation the accession number is in the given list!\n";
			$match = 1;
		}
		else
		{
			$match=0;
		}
	}
	if (!$match)
	{
		print "nothing found!\n"
	}
}

For eg. my input file looked like
#"clean1.acc"
1
2
3
4

I dont know what I am doing wrong but i can figure out that i have to save the found number ......?

Thanx

you had misplaced the if(!$match) block.

Hope this helps you,


Regards,
kath

commented: really helpful and understanding answers +1

A simpler way:

while($accessionno ne "STOP")
{
	print " which Accession number are you searching for? ";

	chomp ($accessionno= <STDIN>);

        if (grep {$_ eq $accessionno} @arr) {
                print "congratulations the accession number is in the given list\n";
        }
	else {
		print "nothing found!\n"
	}
}

but overall that is some pretty useless code unless all you need is to know if the number is found or not.

Hey, Kevin... thats pretty cool code, you included to find the whether element is there in array or not..


I din't knew that technique, thanks...

Thank you,
Regards,
kath.

You're welcome. grep is used a lot in perl for testing lists and creating lists. 'map' is another useful function for creating lists.

Hey Kevin and kath...
Thanx ALOT for ur reply, its really cool to count with daniweb guys :lol:
I had combined my solution through ur answers:

use strict;
open(IN, '<', 'clean5.acc') or die "Could not read file\n";
my @tab = <IN>;
close IN;
# Ask for accession no
print "Enter accession numbers or STOP: ";
my $accession;
while ("STOP\n" ne ($accession = <STDIN>)) {
# Simple linear search
my $found = 0;
foreach my $acc (@tab) {
$found++ if $acc eq $accession;
}
chomp $accession;
if ($found == 0) {
print "$accession is not in the list\n";
}
else {
print "$accession is in the list\n";
}
print "Enter another accession no: ";
}

Again Thank you soooooo much, peace :cheesy:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.