lecb has asked for the wisdom of the Perl Monks concerning the following question:
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
strands.txtapple cherry peach banana cherry
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?apple + cherry - cherry + peach + banana -
Any help would be gratefully received. - E#!/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"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unique key identifer?
by roboticus (Chancellor) on Jun 27, 2014 at 17:49 UTC | |
|
Re: Unique key identifer?
by Laurent_R (Canon) on Jun 27, 2014 at 21:17 UTC | |
by LanX (Saint) on Jun 27, 2014 at 21:44 UTC | |
by Laurent_R (Canon) on Jun 27, 2014 at 23:00 UTC | |
by roboticus (Chancellor) on Jun 28, 2014 at 00:21 UTC | |
by Jim (Curate) on Jun 29, 2014 at 04:06 UTC | |
by Laurent_R (Canon) on Jun 29, 2014 at 15:33 UTC | |
by lecb (Acolyte) on Jun 28, 2014 at 12:41 UTC | |
by Laurent_R (Canon) on Jun 28, 2014 at 19:10 UTC | |
|
Re: Unique key identifer?
by Bodger (Acolyte) on Jun 28, 2014 at 15:12 UTC | |
|
Re: Unique key identifer?
by 1s44c (Scribe) on Jun 28, 2014 at 17:26 UTC |