Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by graff (Chancellor)
on Dec 24, 2008 at 11:32 UTC ( [id://732452]=note: print w/replies, xml ) Need Help??


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

Dude, you are really coming across as being profoundly clueless. This thread is seventh one you've started on this same basic programming task. (For those keeping score, the previous six, in chronological order, are: Regex problem, Extracting Locations in 3 window size, Debug help, Character replacement, Missing to catch 70th character in 3 window size, and Multiple Key Problem help.)

A common attribute of all these threads is that you have never given a clear description of what you are really trying to accomplish. What exactly are the specs for this job? When you say:

I want my Output should be like this:-
155369268 17 CTA 155369268 70 CGA 269212605 120 TTG

HOW DO YOU KNOW that this is what the output should be? What are the principles for determining what the program is supposed to locate in these files? If you can form a clear, unambiguous answer to that question (in the form of a "cookbook recipe"), it will be a lot easier to get the program to do what is needed.

I'm actually somewhat skeptical about the particular values you are citing here as the "desired" output. Where is the "120" supposed to come from, given that File1.txt does not contain this number?

As for the particular version of noise you've added in this OP to code that others have suggested to you, I don't understand why you have added a "$ref" variable in this while loop:

while (<$fh>) { chomp; my ($key, $pos) = split /\s+/; if (!$positions{$key}) { $positions{$key} = [$pos]; } else { my $ref = $positions{$key}; push @$ref,$pos; } }
(I have undone your randomization of the white-space and indentation.) So, you apparently do not realize that the "else" block there does nothing at all except to push an element onto an array ref, which then goes out of scope (disappears completely, ceases to exist) when you step out of that "else" block. I also don't understand why you are using array refs at all in this while loop.

I think it's time for you to confess that you really don't understand what you are trying to do, let alone what you are actually doing. If it's important for this job to get done, someone else is going to have to do it. Go to your supervisor or advisor or other knowledgeable person who can speak to you face-to-face, and admit to them that you are lost.

It looks like the kind of help you need is not the kind of help you can get from perlmonks.

Replies are listed 'Best First'.
Re^2: Hashs of hash (multiple value) key problem.
by u671296 (Sexton) on Dec 24, 2008 at 14:13 UTC
    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.
      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://732452]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-23 15:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found