I have some problems reading a file into a hash in Perl.
Chr1_supercontig_000000000 1 500
PILOT21_588_1_3_14602_59349_1
Chr1_supercontig_000000001 5 100
PILOT21_588_1_21_7318_90709_1
PILOT21_588_1_43_18803_144592_1
PILOT21_588_1_67_13829_193943_1
PILOT21_588_1_42_19678_132419_1
PILOT21_588_1_67_4757_125247_1
...
So I have this file above. My desired output is a hash with the "Chr1"-lines as key, and the "PILOT"-lines as values.
Chr1_supercontig_000000000 => PILOT21_588_1_3_14602_59349_1
Chr1_supercontig_000000001 => PILOT21_588_1_21_7318_90709_1, PILOT21_588_1_43_18803_144592_1,...
As far as I know, multiple values can be assigned to a key only by reference, is that correct?
I got stuck at this point and need help.
You can read the file line-by-line keeping track of the current hash key:
Here is another fairly short, clear version:
And to print the results:
This is a shorter print-results (but the first one seems more readable to me):
One-liner from command line:
Try this ,
print Dumper $hash;
Many good answers already, so I'll add one that does not rely on regexes, but rather on that the key-lines contain three space/tab delimited entries, and the values only one.
It will automatically strip leading whitespace and newlines, and so is somewhat convenient.
How about:
output:
You are right, the hash values need to be references that point to arrays which contain the PILOT lines.
Here's a way to do it: