Sponsored Content
Top Forums UNIX for Advanced & Expert Users Perl loop txt and check if a hash key Post 302385739 by eng_shimaa on Saturday 9th of January 2010 12:53:13 PM
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;
                         }
}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
DBI::Format(3)						User Contributed Perl Documentation					    DBI::Format(3)

NAME
DBI::Format - A package for displaying result tables SYNOPSIS
# create a new result object $r = DBI::Format->new('var1' => 'val1', ...); # Prepare it for output by creating a header $r->header($sth, $fh); # In a loop, display rows while ($ref = $sth->fetchrow_arrayref()) { $r->row($ref); } # Finally create a trailer $r->trailer(); DESCRIPTION
THIS PACKAGE IS STILL VERY EXPERIMENTAL. THINGS WILL CHANGE. This package is used for making the output of DBI::Shell configurable. The idea is to derive a subclass for any kind of output table you might create. Examples are o a very simple output format as offered by DBI::neat_list(). "AVAILABLE SUBCLASSES". o a box format, as offered by the Data::ShowTable module. o HTML format, as used in CGI binaries o postscript, to be piped into lpr or something similar In the future the package should also support interactive methods, for example tab completion. These are the available methods: new(@attr) new(\%attr) (Class method) This is the constructor. You'd rather call a subclass constructor. The construcor is accepting either a list of key/value pairs or a hash ref. header($sth, $fh) (Instance method) This is called when a new result table should be created to display the results of the statement handle $sth. The (optional) argument $fh is an IO handle (or any object supporting a print method), usually you use an IO::Wrap object for STDIN. The method will query the $sth for its NAME, NUM_OF_FIELDS, TYPE, SCALE and PRECISION attributes and typically print a header. In general you should not assume that $sth is indeed a DBI statement handle and better treat it as a hash ref with the above attributes. row($ref) (Instance method) Prints the contents of the array ref $ref. Usually you obtain this array ref by calling $sth->fetchrow_arrayref(). trailer (Instance method) Once you have passed all result rows to the result package, you should call the trailer method. This method can, for example print the number of result rows. AVAILABLE SUBCLASSES
First of all, you can use the DBI::Format package itself: It's not an abstract base class, but a very simple default using DBI::neat_list(). Ascii boxes This subclass is using the Box mode of the Data::ShowTable module internally. Data::ShowTable(3). Raw Row is written without formating. Columns returned in comma or user defined separated list. String Row is written using a string format. Future releases will include th ability set the string format. AUTHOR AND COPYRIGHT
This module is Copyright (c) 1997, 1998 Jochen Wiedmann Am Eisteich 9 72555 Metzingen Germany Email: joe@ispsoft.de Phone: +49 7123 14887 The DBD::Proxy module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
DBI::Shell(3), DBI(3), dbish(1) perl v5.8.0 2002-11-29 DBI::Format(3)
All times are GMT -4. The time now is 10:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy