sandrider has asked for the wisdom of the Perl Monks concerning the following question:
Question 1
open INFILE, "$ARGV[0]"; @aTryptic = <INFILE>; close INFILE; shift @aTryptic; for ($i=0;$i<@aTryptic;$i++) { $accSeq = SplitFields($aTryptic[$i], 'split'); $aTryptic[$i] =~ s/\r\n$/\n/g; chomp $aTryptic[$i]; $hTryptic{$accSeq} = $aTryptic[$i]; $aTryptic[$i] = $accSeq; } open INFILE, "$ARGV[1]"; @aSemiTryptic = <INFILE>; close INFILE; shift @aSemiTryptic; for ($i=0;$i<@aSemiTryptic;$i++) { $accSeq = SplitFields($aSemiTryptic[$i], 'split'); $aSemiTryptic[$i] =~ s/\r\n$/\n/g; chomp $aSemiTryptic[$i]; $hSemiTryptic{$accSeq} = $aSemiTryptic[$i]; $aSemiTryptic[$i] = $accSeq; }
What it does is just open a file passes into an array, then replace each cell with some part of the whole and also create a hash.
Question is, how can I make it into a sub so that I don't have a duplicate?
Question 2
2 files of data (strings of data that is separated by tabs, e.g. name\taddress\tetc). I need to compare the 2 I create 2 arrays and 2 hashes, the arrays contain just the field that I want to compare e.g. name and the hashes are created with the name as key and the rest is the values.
I compare the 2 arrays and if there is a common name I'll get the value from 1 hash and print in into a file, if there is none then I get the value from the other hash and print into the same file.
foreach $one (@arrayA) { chomp $one; $found = 0; foreach $two (@arrayB) { chomp $two; if ($one eq $two) { print "$hashB{$two}"; $found = 1; last; } } if ($found == 0) { print "$hashA{$one}"; } }
Is this the best way to do it?
Thanks.
Desmond
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can I make a sub for this?
by Zaxo (Archbishop) on Aug 22, 2005 at 11:22 UTC | |
|
Re: Can I make a sub for this?
by tilly (Archbishop) on Aug 22, 2005 at 17:00 UTC | |
|
Re: Can I make a sub for this?
by eric256 (Parson) on Aug 22, 2005 at 20:08 UTC | |
by sandrider (Acolyte) on Aug 23, 2005 at 03:22 UTC |