如何在 Perl 中将字符串读入哈希

How do I read strings into a hash in Perl

我有一个文件,里面有一系列随机的 A、G、C 和 T,看起来像这样:

>Mary
ACGTACGTACGTAC
>Jane
CCCGGCCCCTA
>Arthur
AAAAAAAAAAT

我把这些字母连接起来,以 ACGTACGTACGTACCCCGGCCCCTAAAAAAAAAAT 结尾。我现在在那个串联序列中有一系列我感兴趣的位置,我想找到与这些位置(坐标)匹配的关联名称。我使用 Perl 函数 length 来计算每个序列的长度,然后将累积长度与散列中的名称相关联。 到目前为止我有:

#! /usr/bin/perl -w
use strict;

my $seq_input = $ARGV[0]; 
my $coord_input = $ARGV[1]; 
my %idSeq; #Stores sequence and associated ID's.

open (my $INPUT, "<$seq_input") or die "unable to open $seq_input";
open (my $COORD, "<$coord_input") or die "unable to open $fcoord_input";

while (<$INPUT>) {
    if ($_ = /^[AGCT/) {
    $idSeq{$_

my $id = ( /^[>]/) 

#put information into a hash
#loop through hash looking for coordinates that are lower than the culmulative length

foreach $id
 $totallength = $totallength + length($seq)
 $lengthId{$totalLength} = $id
foreach $position
 foreach $length
  if ($length >= $position) { print; last }

close $fasta_input;
close $coord_input;
print "Done!\n";

到目前为止,我无法将文件读入哈希。我还需要一个数组来打印哈希吗?

不完全清楚你想要什么;也许是这样的:

my $seq;
my %idSeq;
while ( my $line = <$INPUT> ) {
    if ( my ($name) = $line =~ /^>(.*)/ ) {
        $idSeq{$name} = length $seq || 0;
    }
    else {
        chomp $line;
        $seq .= $line;
    }
}

产生:

$seq = 'ACGTACGTACGTACCCCGGCCCCTAAAAAAAAAAAT';
%idSeq = (
      'Mary' => 0,
      'Jane' => 14,
      'Arthur' => 25,
);