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
# 1  
Old 03-01-2009
Perl Hashes, reading and hashing 2 files

So I have two files that I want to put together via hashes and am having a terrible time with syntax. For example:

Code:
File1

A    apple
B    banana
C    citrus

Code:
File2

A    red
B    yellow
C    orange

What I want to enter on the command line is:

Code:
program.pl File1 File2

And have the result print out

Code:
A    apple    red
B    banana    yellow
C    citrus    orange

What I have been trying to write up in Perl, is a program that is basically as follows :

Code:
foreach line in ARGV[0]
    split the line by tabs
    assign key and value

foreach line in ARGV[1]
    split the line by tabs
    assign key and value

for each key from ARGV[0]
    print key, value, ARGV[1](key)


The code that I've been trying to troubleshoot is:

Code:
  1 #!/usr/bin/perl -w
  2 use diagnostics;
  3 use warnings;
  4 
  5 %affyMap = ();
  6 %affyAnnot = ();
  7 @map = <$ARGV[0]>;
  8 @annotation = <$ARGV[1]>;
  9 $line = "";
 10 
 11 foreach $line (@map){
 12         chomp($line);
 13         my($ID,$info) = split(/\t/,$line,2);
 14         $affyMap{$ID} = $info;
 15 }
 16 
 17 foreach $line (@annotation){
 18         chomp($line);
 19         my($ID,$info) = split(/\t/,$line,2);
 20         $affyAnnot{$ID} = $info;
 21 }
 22 
 23 foreach $key (%affyMap){
 24         print "$key\t" . "$affyMap{$key}\t" . "$affyAnnot{$key}\n";
 25         }

However, when I put a print in the while statement, it doesn't seem to be even making a hash at all since it only reports one "print". I suspect the issue is with the way I am passing in variables, but am now stumped.

I have tried replacing the "foreach $line (@map)" with a "while $line ($ARGV[0])" but it still isn't making the hash tables from what I can tell.

Any help or pointers would be awesome. Thanks!
# 2  
Old 03-01-2009
lines 7 and 8 are wrong. The <> operator reads from a filehandle, not a filename. Look up the proper use of @ARGV and while(<>). You can find @ARGV in perlvar.

perlvar - perldoc.perl.org
# 3  
Old 03-01-2009
I'll search though that link and see if I can't make some sense out of it. Lots of documentation goes over my head still. Probably explains why syntax gets me every time. Thanks!
# 4  
Old 03-01-2009
why not use
Code:
join file1 file2

?
# 5  
Old 03-01-2009
I'm under the impression that join will just append line for line? I am trying to use the hash just in case "file 1" doesn't contain all the elements found in "file2".

I'm working with a large data set from which has a matching file with what they are. Instead of going into excel and manually searching for the item from file1 and copying the respective data from file2, I wanted a script to do it for me, if what I'm trying to do makes any sense.

ETA:
LOL At this rate, it might have been faster for me to go though the few hundred ID's and manually serach, copied, and pasted. If I started doing that when I started trying to do this, I might be finished by now. But I want to get this to work since it would be a one time thing. Thanks for the suggestions!

Last edited by silkiechicken; 03-01-2009 at 10:00 PM..
# 6  
Old 03-02-2009
The reason I did not post any code for you is because your question looks like school work.
# 7  
Old 03-02-2009
That's fine and understandable. I read the user agreement that we can't ask you all do our "hw" for us. I have no problem with trying to read up and figure it out, just need some direction on where to look when the Perl help isn't really help. All it was giving me was that I had an unassigned variable?

It's kind of "school work" in that it will ultimately be used for my thesis research. I'm working on my PhD in Molecular and Cellular Biology and our lab has a mouse microarray data set, for which our post doc is currently manually searching though the sets by hand! Background is in bioengineering, so I didn't feel that it was a very efficient method to go data mining. I've put the data though SAMExcel to find the genes from the array which had a 1.4 fold change and have an associated list of affyID's, but need to connect them with the data found in the affyannotation files. I wish Affy would just use the accession numbers so we could look them up on NCBI or MGI directly, but that's the way it is. I'll stop rambling now though. LOL

I'll keep playing with it! Thanks for the help and suggestions!
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