in reply to Re^3: Key value pair in hash inside while loop
in thread Key value pair in hash inside while loop

Hi,

Thank you for your reply. The expected output would be listed like below:

 GTTCTTATTTGCACCTACT    180

for simplicity I have truncated the data file 2 at 300 number, but in actuality, the list may have 3k - 4k numbers.

Regards

Replies are listed 'Best First'.
Re^5: Key value pair in hash inside while loop
by Cristoforo (Curate) on Aug 28, 2014 at 18:59 UTC
    If that's all you want for output, the following would do that.
    #!/usr/bin/perl use strict; use warnings; use Inline::Files; my %data; while (<A>) { chomp; my (undef, $fragment, undef, $pos) = split /\t/; $data{$pos} = $fragment; } <B>; # throw away header '300 dG = -62.54 ...' while (<B>) { my $pos = (split /\t/)[0]; print "$data{$pos} $pos\n" if $data{$pos}; } __A__ 1-Match: GGTGTTGTATGCCTTTAAA 5 3545 2-Match: GGAAAGTCAAGCCCATCTA 9 3254 3-Match: GTTCTTATTTGCACCTACT 6 180 4-Match: GATGAGGAACAGCAACCTT 5 844 __B__ 300 dG = -62.54 [initially -70.70] gi178893_M23263_rna_300-1 1 G 0 2 0 1 2 A 1 3 0 2 3 A 2 4 0 3 4 U 3 5 0 4 5 U 4 6 0 5 6 C 5 7 0 6 7 C 6 8 0 7 8 G 7 9 34 8 9 G 8 10 33 9 ........ ........ 179 A 178 180 231 179 180 G 179 181 230 180 181 U 180 182 229 181 ........ ........

      Thanks Cristoforo,

      I modified the code as suggested by you but I thing the program is giving me error of uninitialized values.

      #!/usr/bin/perl -w + use strict; use warnings; my $matchfile = "match_super.dat"; my $ctfile = "M23263_rna_300-1.dat_1.ct"; my @match;my %data;my $fragment; my $pos; open (A, "<", $matchfile) or die "Check the file $!\n"; while (my $line = <A>) { chomp $line; (undef, $fragment, undef, $pos) = split(/\t/, $line); $data{$pos} = $fragment; } close (A); my ($num1, $base, $num2, $num3, $connect, $num4); open (B, "<", $ctfile) or die "Check the file $!\n"; while (my $ctline = <B>) { chomp $ctline; if ($ctline =~ /^(\d+\s+dG.*)/) { # print "$1\n"; next; + next; } $pos = (split /\s+/)[0]; print "$data{$pos} $pos\n" if $data{$pos}; } close (B);
      Error: Use of uninitialized value $pos in hash element at ./second_str_map.pl + line 29, <B> line 298. Use of uninitialized value $_ in split at ./second_str_map.pl line 28, + <B> line 299. Use of uninitialized value $pos in hash element at ./second_str_map.pl + line 29, <B> line 299. Use of uninitialized value $_ in split at ./second_str_map.pl line 28, + <B> line 300. Use of uninitialized value $pos in hash element at ./second_str_map.pl + line 29, <B> line 300. Use of uninitialized value $_ in split at ./second_str_map.pl line 28, + <B> line 301. Use of uninitialized value $pos in hash element at ./second_str_map.pl + line 29, <B> line 301.
        Change

        $pos = (split /\s+/)[0];

        to

        $pos = (split /\s+/, $ctline)[0];