Perl Hashes, reading and hashing 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Hashes, reading and hashing 2 files
# 8  
Old 03-02-2009
In that case, see is this helps:

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

my ($file1,$file2) = @ARGV[0,1];
unless ($file1 && $file2) {
   print "Usage : program_name file1 file2\n";
   exit(0);
}	 
my %affyMap = ();
my %affyAnnot = ();

open (my $F1, $file1) or die "$!";
my @map = <$F1>;
close $F1;
open (my $F2, $file2) or die "$!";
my @annotation = <$F2>;
close $F2;

foreach my $line (@map){
        chomp($line);
        my($ID,$info) = split(/\t/,$line,2);
        $affyMap{$ID} = $info;
}

foreach my $line (@annotation){
       chomp($line);
       my($ID,$info) = split(/\t/,$line,2);
       $affyAnnot{$ID} = $info;
}

foreach my $key (keys %affyMap){
       print "$key\t$affyMap{$key}\t$affyAnnot{$key}\n";
}


Last edited by KevinADC; 03-02-2009 at 07:33 PM..
# 9  
Old 03-02-2009
Thanks!

I'll go make a copy of the current file and make the changes to see if it works with your modifications. I actually played with it more last night and switched to reading in the files with open and using a while, but it still failed to open for some reason. Entirely baffles once again, and I know the files are there, because I can open them with "more file_name".

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

my %affyMap = ();
my %affyAnnot = ();

open (AFFYMAP, "<$ARGV[0]>") || die "$!: Can't open ARGV0. . . exiting\n";

while (<AFFYMAP>){
        chomp($_);
        my($ID,$info) = split(/\t/,$_,2);
        $affyMap{$ID} = $info;
}
close (AFFYMAP);

open (ANNOTATION, "<$ARGV[1]>") || die "$!: Can't open ARGV1 . . . exiting\n";

while (<ANNOTATION>){
        chomp($_);
        my($ID,$info) = split(/\t/,$_,2);
        $affyAnnot{$ID} = $info;
}

close (ANNOTATION);

foreach my $key (%affyMap){
        print "$key\t" . "$affyMap{$key}\t" . "$affyAnnot{$key}\n";
        }

Thanks again for all your help! I'll let you know if I can get it to work.
# 10  
Old 03-02-2009
I don't know what $ARGV[0] is but your usage is still a bit off. Remove the <> in the open argument:

Code:
open (AFFYMAP, $ARGV[0]) or die "$!: Can't open $ARGV[0]. . . exiting\n";

also you need to use "keys" when looping through the hash:

Code:
foreach my $key (keys %affyMap){

There are a few ways to loop through a hash but for your application "keys" will work.
# 11  
Old 03-02-2009
Thanks!

I think I know where I missed up with the arrows, the example I was following had a single "<" as in to read a file, but for some reason I added a ">" on the other end. As for the keys thing, learn something new every day!!! Well, actually two things, since I know know there is a function called unless.

I have both versions of the code working now, or so I think, so after some more testing tonight to make sure it really is doing what I want, I'll be on to finding a particular segment of text in each line, and collecting the output. Can't be that hard right. LOL

Thanks again for all your help!!!
# 12  
Old 03-03-2009
You're welcome.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl hash of hashes anonymous array

Hello experts. I'm having problems with a snippet of code. I was hoping to get help/advice to correct. A file that this script parses has changed to the point where I can no longer use a scalar, it looks as though I need to create an array for a hash of hashes below. The first output of... (1 Reply)
Discussion started by: timj123
1 Replies

2. Shell Programming and Scripting

Perl : array of hashes help

Hi, I have array of hashes and each key has array like below. @array1 = ( { 'url' => , 'bill' => }, { 'url' => , 'bill' => }, { 'url' => , ... (0 Replies)
Discussion started by: ragilla
0 Replies

3. Shell Programming and Scripting

perl: dereferencing a hash of hashes

Hi there, I am trying to dereference my hash of hashes but post dereferencing, it seems to lose its structure I am using Data::dumper to help me anaylise. This is the code im using to build the HoH, (data comes from a file). I have also performed a Dumper on the data structure before and after... (1 Reply)
Discussion started by: rethink
1 Replies

4. Shell Programming and Scripting

PERL - another quick hash of hashes question

Hi, sorry, two hash related questions in one day .. but this has got me a bit stuck. I have a mysql database table that kind of looks like this, the table is called "view1" and a snippet of that table (SELECT'ing just rows with serial number 0629AN1200) is below serial nic_name ... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

5. Shell Programming and Scripting

PERL - printing a hash of hashes to screen

Hi there I have a hash of hashes made up of the following data bge0|100|half|10.36.100.21 bge1|1000|full|10.36.100.22 bge2|1000|full|10.36.100.23 which when i turn into a hash, would look like this inside the system bge0 -> nic_speed -> 100 nic_duplex -> half ... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

6. Shell Programming and Scripting

perl hash of hashes from database

hi there, I have some database output that looks like this SELECT nic_name,nic_duplex,nic_speed,nic_ip FROM network_table WHERE hostname = "server1" result is this (ive delimited with a pipe for ease of reading) bge0|full|1000|10.32.100.1 bge1|full|1000|11.12.101.7 ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

7. Shell Programming and Scripting

Hashes help in Perl

Hi, I am stuck at this problem where part of my code would store all the websites that has been accessed by a user. I pull these values from a log file. I want to create a HASH of HASHES ? (Please correct me if this is not the right approach) where I would store all the hits to website with... (4 Replies)
Discussion started by: Dabheeruz
4 Replies

8. Shell Programming and Scripting

perl hashes question

hi guys im running into a problem here this is my script #!/usr/bin/perl use CGI qw(:standard); $header = "MIME-Version: 1.0\n"; $header .= "Content-type: text/html\n"; $header .= "\n"; #get the point parameter from nhl.html $Team = param("points"); print "$header"; open(INFILE,... (1 Reply)
Discussion started by: lucho_1
1 Replies

9. Shell Programming and Scripting

How to declare hashes in KSH similar to Perl ?

Hi, Is it possible to delcare hashes in KSH the way we do it in Perl. Like I want to declare something like: fruits="Juicy" fruits="healthy" fruits="sour" echo fruits Ofcourse this piece of code does not work in KSH. Please let me know if there is a way of doing it in KSH. ... (2 Replies)
Discussion started by: tipsy
2 Replies

10. Shell Programming and Scripting

perl: hashes, whiles, and last

hello everyone, i am creating 2 hashes from 2 different files, then looking for the value of one in the value of the other to make a new file. for example: file1: DENV => Denver file2: H224-0A-12 => DENVER if Denver is found in DENVER (case insensitive), a new hash now contains H224-0A-12... (0 Replies)
Discussion started by: effigy
0 Replies
Login or Register to Ask a Question