b00mIR has asked for the wisdom of the Perl Monks concerning the following question:

I've created a hash which looks as follows

keys values 5302 0 C 1 T 9046 0 G 1 A 11110 0 T 1 C 12640 0 T 1 C 18262 0 A 1 G 21994 0 A 1 G 25713 0 A 1 G 26638 0 T 1 G 28194 0 A 1 G 29841 0 G 1 A 30475 0 T 1 C 31503 0 A 1 C 32211 0 G 1 A 37138 0 G 1 A 41088 0 G 1 A 42626 0 A 1 G

using this I was hoping to replace values in another file which looks as so.

# ID 1005.csv 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 0 0 1... 0 1 1 1 0 1 1 0 1 1 1 0 0 0 0 0 1 1 0... # ID 1009.csv 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 0 0 1... 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 0 0 1... etc...

each line of the hash represents the values of the file i want to alter in order i.e. the first line of the hash represents the first value in each line, the second line of the hash represents the second value in each line.

What I would like to achieve is at each position of each line replace the value (either 1 or 0) with the letter code that is represented by it's related line on the hash.

I've been bashing at this for days without any luck.

Replies are listed 'Best First'.
Re: Using Hashes to replace values
by JavaFan (Canon) on Mar 07, 2012 at 11:25 UTC
    Your sample output contains 76 0's and 1's. I count 32 0's and 1's in the input, one for each line. The input also contain 32 letters from the set A C G T, and 16 multi digit numbers, none of them appearing in the output.

    I cannot figure out the relation between the input and the output.

Re: Using Hashes to replace values
by choroba (Cardinal) on Mar 07, 2012 at 11:13 UTC
    I do not get it. Can you show the desired output for your sample input? Also, can you explain where the hash key comes from and what its relation to the data is?

      my desired output would be like this

      # ID 1005.csv C A C C A G G G G A C A A A A G G A etc.. C A C C A G G T G A C A C G G G A G etc.. etc...

      the hash extends on for about another 1300 keys or so and the file that is being changed has about 800 more IDs<\p>

        #!/usr/bin/perl use warnings; use strict; my %hash = ( '5302' => [qw/C T/], '9046' => [qw/G A/], '11110' => [qw/T C/], '12640' => [qw/T C/], '18262' => [qw/A G/], '21994' => [qw/A G/], '25713' => [qw/A G/], '26638' => [qw/T G/], '28194' => [qw/A G/], '29841' => [qw/G A/], '30475' => [qw/T C/], '31503' => [qw/A C/], '32211' => [qw/G A/], '37138' => [qw/G A/], '41088' => [qw/G A/], '42626' => [qw/A G/]); my @keys = sort { $a <=> $b } keys %hash; while (<DATA>) { unless (index $_, '#') { print; next; } my $idx = 0; print join ' ', map {$hash{ $keys[$idx++] }[$_]} split; print "\n"; } __DATA__ # ID 1005.csv 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 0 1 1 1 0 1 1 0 1 1 1 0 0 0 0 0 # ID 1009.csv 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0
        I had to truncate the input since there are not enough entries in the sample hash.
Re: Using Hashes to replace values
by Anonymous Monk on Mar 07, 2012 at 11:14 UTC
Re: Using Hashes to replace values
by roboticus (Chancellor) on Mar 07, 2012 at 13:03 UTC
    I've been bashing at this for days without any luck.

    And yet you don't show us the code, nor tell us what problem you're having. You might think a bit about how to more clearly ask a question that might elicit a helpful response.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.