Dear Monks, I need you wisdom again.

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


In reply to Can I make a sub for this? by sandrider

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.