Perl loop txt and check if a hash key


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Perl loop txt and check if a hash key
# 1  
Old 01-09-2010
Perl loop txt and check if a hash key

Hi,
The task i have to do is to
1- create a database contains the Names .run the query and store results in hash make the Name field is the hash key
2- in the same time i have a txt which i will loop through it word by word and check for each word if a hash key ( compare it with the Names in the Name field in the hash ) or not if it is a key print it to a txt file and print that it match the database.

Now , the code can get the results from db in a hash form with the key field Name andin the same time loop through the input txt word by word

But till now i can't compare these words withthee names stored in the hash to know if it match or not.

I am abeginner and really i arrive to this level after alot of efforts and i can't go ahead with code any more. So really i need your help.

Code:
#!/usr/bin/perl
use DBI;
 
               #step1
  $sqlstatement = "select Name from protiens;";
     #open connection to Access database  
   $dbh = DBI->connect('dbi:ODBC:driver=microsoft access driver (*.mdb);dbq=C:\protiens.mdb') ;
      #prepare and execute SQL statement 
$sth = $dbh->prepare ($sqlstatement);
$sth->execute || die "Could not execute ";
$ref1 = $sth->fetchrow_hashref($Name);
while($ref1 = $sth->fetchrow_hashref($Name)){
print "$$ref1{'Name'}\n";
}
 
           # step2
%hash1 = $$ref1{'Name'};
if(exists $hash1{$key}){
print "there is a key\n";
}
 
            #step3
$filename= 'C:\project\abstract of protein.txt';
open (FILE,$filename) or die $!;
while (<FILE>){
  for $word (split)
  { 
      # define hash
%hash2 = $word;
foreach $key (%hash2) {
      #Check for a hash key
if(exists $hash2{$key}){
print "there is a one key\n";
}
}
 
 }        #step4 unknown
   if ($ref1{$key} eq $hash2) {
       print "The key for $hash2 is $key\n";
          $flag++;
                               }
 
         unless ($flag) {
           print "There is no key for $hash2".\n;
                         }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to store info from a txt file into a hash?

I'm trying to make a perl script using the "open" command to open and read a file, storing the information in said file into a hash structure. This is what is inside my file- Celena Standard F 01/24/94 Cancer Jeniffer Orlowski F 06/24/86 None Brent Koehler M 12/05/97 HIV Mao Schleich... (4 Replies)
Discussion started by: Eric1
4 Replies

2. Shell Programming and Scripting

Print perl hash value in foreach loop

Experts - Any advice on how to get a hash value in a foreach loop? Values print correctly on standalone print statements, but I can't access value in foreach loop. See sample code below and thanks in advance. foreach my $z (sort keys %hash) { for $y (@{$hash{$z}}) { print "$z... (6 Replies)
Discussion started by: timj123
6 Replies

3. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

4. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

5. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

6. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

7. Shell Programming and Scripting

Assigning a hash to another hash key

Hello, I have a hash in hsh. I need to assign it to another hash globalHsh. I think the below statement does not work $globalHsh{$id} = %hsh; What is the right way to assign it? Thanks (3 Replies)
Discussion started by: rsanjay
3 Replies

8. Shell Programming and Scripting

perl (word by word check if a hash key)

Hi, Now i work in a code that 1-get data stored in the database in the form of hash table with a key field which is the " Name" 2-in the same time i open a txt file and loop through it word by word 3- which i have a problem in is that : I need to loop word by word and check if it is a... (0 Replies)
Discussion started by: eng_shimaa
0 Replies

9. Shell Programming and Scripting

PERL: if($key == "a" ), how to check alphas!

hello again, i've checked many sites for examples and am amazed that i can't find a simple example of an if statement to test for an alphanumber value ie: if($key == "a" ) to run this pgm: perl del3.pl delin delin data is: a=1 b=2 c=3 thanks for any pointers ps: java programmer... (7 Replies)
Discussion started by: bobk544
7 Replies

10. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies
Login or Register to Ask a Question