Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Hashs of hash (multiple value) key problem.

by u671296 (Sexton)
on Dec 24, 2008 at 14:13 UTC ( [id://732465]=note: print w/replies, xml ) Need Help??


in reply to Re: Hashs of hash (multiple value) key problem.
in thread Hashs of hash (multiple value) key problem.

The $ref was added at my suggestion as the keys in file 1 are not unique, so an array is needed to store the varying $pos values that $key can have. The suggested code produced exactly the result requested in the "Multiple Key Problem Help" thread.

I agree with the rest of your response. Way too many threads for some very basic problems. A bit of googling would answer the problem raised this time.
  • Comment on Re^2: Hashs of hash (multiple value) key problem.

Replies are listed 'Best First'.
Re^3: Hashs of hash (multiple value) key problem.
by ashnator (Sexton) on Dec 25, 2008 at 09:19 UTC
    Hi modified the program but it is still giving some errors :(
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $qfn1 = "File1.txt"; my $qfn2 = "File2.txt"; my %positions; { open(my $fh, '<', $qfn1) or die("Cannot open file \"$qfn1\": $!\n"); while ( <$fh> ) { chomp; my ( $key, $pos ) = split /\s+/; push @{ $positions{$key} }, $pos; } } print Data::Dumper->Dumpxs( [ \ %positions ], [ qw{ *positions } ] ); my %sequences; { open(my $fh, '<', $qfn2) or die("Cannot open file \"$qfn2\": $!\n"); my $key; while (<$fh>) { if ( s/^>// ) { $key = ( split /\|/ )[1]; } else { chomp; $sequences{$key} .= $_; } } } for my $key (sort keys %positions) { for my $key (keys %{$positions{$key}}) { foreach my $value (@) { if (! exists( $sequences{$key} )) { warn "KEY $key in File1 not found in File2\n"; next; } if ( length( $sequences{$key} ) < $positions{$key} ) { warn "KEY $key: File2 string length too short for File1 positi +on value\n"; next; } my $index = rindex( $sequences{$key}, "ATG", $positions{$key} ); if ( $index < 0 ) { warn sprintf( "KEY %s: No ATG in File2 string prior to positio +n %d\n", $key, $positions{$key} ); next; } $index += 3 while ( ($index + 3) < $positions{$key} ); print "$key $positions{$key} " . substr($sequences{$key}, $index, +3) . "\n"; } } }

      Please check your script for syntax errors:

      $ perl -c script.pl syntax error at /tmp/test.pl line 45, near "if" syntax error at /tmp/test.pl line 61, near "}" $

      some comments upon your code.

      • whole code: a clear indentation would help reading and debugging the code! perltidy can help with this!
      • line 31: why do you use s/// here? m// should do the job! The replacement of > (at the beginning of the string) is not needed here as you split the string and use the second part. So why do you remove the >?
      • line 42: %positions is a Hash-of-Arrays; why are you trying to dereference it as a Hash-of-Hashes?
      • line 44: what's that single @? Maybe you want to dereference the array reference in $positions{$key}? If so, you still should rename $value to $position!
      • What do you think $value is for, as you don't use it in your for-loop.
      • line 49: why are you still comparing a string length with an array reference?
      • I stopped analyzing the following lines. But it looks like as you are not aware about what you have stored in $positions{$key}

      As tilly already mentioned, you should start learning about perl's complex data structures. Recommended documentation:

      Update:

      • comment on line 42 added.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://732465]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 12:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found