Good evening Monks!
I am struggling to deal with a case when a hash key has two values. I have written a basic program to try and illustrate what I am trying to do.
fruit.txt

apple cherry peach banana cherry
strands.txt
apple + cherry - cherry + peach + banana -
Using the fruit.txt file, I would like to be able to print 'cherry' if it is matched as positive, as well as if it is negative. Is there anyway I can do this in perl?
My program:
#!/usr/bin/perl -w use strict; use Data::Dumper; my $inputfile1 = $ARGV[0]; my $inputfile2 = $ARGV[1]; open (FILE1, $inputfile1) or die; open (FILE2, $inputfile2) or die; my @fruit = <FILE1>; # fruit.txt close FILE1; my @strands = <FILE2>; ## strands.txt close FILE2; my (@fruitcolumn, @strand_direction, %strands, $key, $value); foreach my $line(@strands) { my @colsplit = split("\t", $line); push (@fruitcolumn, $colsplit[0]); push (@strand_direction, $colsplit[1]); } s/\s+$// foreach @strand_direction; s/\s+$// foreach @fruit; while ($key = shift (@fruitcolumn)) { $value = shift (@strand_direction); push @{$strands{$key}}, $value; } foreach my $line (@fruit) { if ($strands{$line} =~ m/\+/) { print "my positives are $line", "\n"; } elsif($strands{$line} =~ m/\-/) { print "my negatives are $line", "\n"; } }
Any help would be gratefully received. - E


In reply to Unique key identifer? by lecb

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.